CreatingImageBeanUsingNetBeans6.1

Contributed by : Jay Mahadeokar.

[[{TableOfContentsTitle=TableOfContents} | {TableOfContents title='Table of Contents'}]]

Java Beans functionality that had been dropped in NetBeans IDE 6.0 has again been provided in IDE 6.1 with some new features.Here we will explore the new features:

For the starters..

NetBeans 6.1 Beta has included the following new bean functionality (as mentioned in the NetBeans IDE 6.1 Release Candidate Information)

  1. Bean Patterns in Navigator
  2. Generate Bean Property
  3. BeanInfo Editor

We will try to find it first hand…

Creating a new Image Bean

We will create a simple bean that will have a function to display any image. The bean created will reside in the palette so that we can drag and drop it just like any other control in the palette. Here’s how we can go about creating the bean.. Starting the project.. For loading and displaying the image, we will make use of JAI API

[[{TableOfContentsTitle=TableOfContents} | {TableOfContents title='Table of Contents'}]]Create a sample java project and name it as ImageBean. Now right click on source package >> select New >> Other >>Java Bean object.

NetBeans 6.1 Beta provides following options for bean development:

  1. Java Beans Component : Creates a new JavaBeans component. The created skeleton code contains a sample property and property change support.
  2. Bean Info: Creates a new BeanInfo class. The BeanInfo is used as a description of how its associated JavaBeans component should behave and appear.
  3. Bean Info w/o icon: It is BeanInfo class without icon.
  4. Customizer: Creates a Customizer panel for a JavaBeans component. The customizer can then be used during design time in the IDE to configure the bean.
  5. Property Editor: Creates a PropertyEditor class. Property editors are used at design time in IDEs to support editing of the properties of JavaBeans components.


Adding the essential code:

  • For simplicity, we will go with the Customizer panel. So, add a new Costomizer panel to our project and name it as ImageBean.

File:bean1_CreatingImageBeanUsingNetBeans6.1.JPG

  • Now add a new jInternalFrame to the panel, rename it as imageFrame.
  • Add the following packages at the top in source tab.
import java.io.File;
import javax.swing.JFileChooser;
import javax.media.jai.PlanarImage;
import com.sun.media.jai.widget.DisplayJAI;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.media.jai.JAI;
import javax.swing.JScrollPane;
  • Add the following private variables to class. These will be required to load and display the image.
//To load the image file
PlanarImage srcImage = null;
//To display the image file
DisplayJAI srcdj = null;
JScrollPane srcScrollPaneImage = null;
//To choose file by browsing
private JFileChooser FileChooser = new JFileChooser();
//This flag will be set once image is loaded for the first time
public boolean flag;
  • Also add the following method to the class. This method will load the image into the bean according to the specified path.
public void displayimage(File path ) {
// Load the image which file name was passed as the first argument to the application.
srcImage = JAI.create("fileload", path.getPath());
//When image is loaded for first time...
if(flag == false ) {
// Get the JFrame's ContentPane.
Container contentPane = imageFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
// Create an instance of DisplayJAI.
srcdj = new DisplayJAI(srcImage);
// Add to the JFrame's ContentPane an instance of JScrollPane containing the
// DisplayJAI instance.
srcScrollPaneImage = new JScrollPane(srcdj);
//srcScrollPaneImage
contentPane.add(srcScrollPaneImage,BorderLayout.CENTER);
this.setVisible(true); // show the frame.
flag = true;
}
//If any image was already loaded before
else {
srcdj.set(srcImage);
}
}

The real fun starts now.

The support provided by NetBeans 6.1 Beta for bean development comes into play now. Right click on the ImageBean class and you will see following options: File:bean2_CreatingImageBeanUsingNetBeans6.1.JPG

Click on the BeanInfoEditor option and following dialog will appear: File:bean3_CreatingImageBeanUsingNetBeans6.1.JPG


Click ok. A ImageBeanInfoEditor will be automatically created for you. Now a new designer view is provided along with source view for the InfoEditor. Switch to designed view and you will see the following: File:bean4_CreatingImageBeanUsingNetBeans6.1.JPG


You can see that the designer shows:

  1. The bean whose info is being shown
  2. The properties of the bean.
  3. Event Sources corresponding to the events that will be handled by our bean.
  4. Methods that our bean has.

Note that the displayimage method has a red cross corresponding to it which specifies that it has not been included in the bean info. You can right click, and say “toggle include” to include it in bean info. File:bean5_CreatingImageBeanUsingNetBeans6.1.JPG

Alternately you can just check the box for including in bean info as shown: File:bean6_CreatingImageBeanUsingNetBeans6.1.JPG


The changes that you make to your bean will be automatically reflected in the bean info editor. If you add properties or event handlers to your bean corresponding changes can be easily checked out in the bean info. Also the bean patterns can be monitored through the navigator which describes all properties and events of bean. File:Bean11.JPG


Compiling and adding the bean to palette.

Now our bean is ready to be used. But before that we must compile it and add it to palette. For that first right-click on ImageBean class and click compile. Now again right-click >> Tools >> Add to palette. File:bean7_CreatingImageBeanUsingNetBeans6.1.JPG


Following dialog will appear. Select Beans and click ok. File:bean8_CreatingImageBeanUsingNetBeans6.1.JPG


Testing our bean:

  • Create a new jFrame class.
  • Go to palette and select our imageBean. You may have to refresh palette so that the bean is visible.

File:bean9_CreatingImageBeanUsingNetBeans6.1.JPG

  • Now after the call to initComponents() method in source view add the following code:

imageBean1.displayimage(new File("C:
pic.jpg")); This will call the display image function that we had added to our bean. Pass the path of image that you want to load in the file object constructor. Now set this class as main class of project and run the project.

Output:

The following output will be seen: File:bean10_CreatingImageBeanUsingNetBeans6.1.JPG


Conclusion:

Thus the new features provided for bean development in NetBeans 6.1 have made making useful bean components a really easy and interesting task. Though we have not explore the whole facilities provided but for a start we can say that it really user friendly and easy. Thanks 6.1!

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo