How to Create & Import a JavaBean Component in NetBeans IDE (Part 1)

DZone 's Guide to

How to Create & Import a JavaBean Component in NetBeans IDE (Part 1)

Learn how to import and create a JavaBean component using NetBeans IDE.

· Java Zone ·
Free Resource

This article provides a simple introduction to creating and using JavaBean components in NetBeans IDE.

Why Read this Tutorial?

NetBeans IDE has a really intuitive panel (the Component Palette) which allows users to easily build and prototype components without having to write lots of code. If you have a Visual component and you want to make it as easy as possible for developers to use, its surprisingly easy to add it to this panel. Here's how we did it...

The tutorial is split up into two parts.

  • Part 1 shows how to turn your existing standard Swing components into a JavaBean component.
  • Part 2 describes how – once you have created your JavaBean – to import into the Matisse GUI Builder

Part 1 – Creating a JavaBean


This tutorial assumes that you have already created the Swing component you want to turn into a JavaBean. For the sake of demonstration purposes I am going to use the PdfHelpPanel we recently developed at IDRsolutions. You can find out more about PDF Help here.

Converting a Swing Component Into a JavaBean

In my example the PdfHelpPanel extends JPanel, however, in order to create a JavaBean, all your Component needs to do is extends JComponent, or any subclass of JComponent.

The only caveat to this is that your class must implement a simple constructor. That is a constructor which takes zero arguments. This is because when you instance the JavaBean (ie when you add it to your form through the Matisse Palette) there needs to be a basic constructor which can be called.

This is how my empty PdfHelpPanel looks.

public class PdfHelpPanel extends JPanel implements Serializable {public PdfHelpPanel() {}}

To allow your JavaBean to be customized externally by the end-user, you need to add properties to your Swing component which conform to the JavaBean architecture - that is where a field is accessible through matching getter and setter methods. For example, the code snippet below would expose the field property

private int field;public void setField(int field) {this.field = field;}public int getField() {return field;}

In my case, I am only creating a very simple JavaBean which contains one property. That is an array of type String that will hold the list of files which will appear in the users File List.

Adding this property to my existing PdfHelpPanel class gives the following class.

public class PdfHelpPanel extends JPanel implements Serializable {public PdfHelpPanel() {}/**  * fileLocations property which can be accessed through the * JavaBean architecture.  */private String[] fileLocations = {""};/** * Sets the locations of the files which will be displayed in the File * List. *  * @param fileLocations Array of locations for the PDF files.<br/><br/> *  * Elements can be: -  * <ul> * <li>An absolute file path of a local location on disk</li> * <li>A directory path which will be scanned recursively,  * adding all PDFs found to the file list</li> * <li>A URL</li> * <li>A classpath location</li> * </ul> *  * Examples: -  * <ul> * <li>"C:/file.pdf"</li> *<li>"C:/PdfFiles/"</li> * <li>"http://www.jpedal.org/jpedal.pdf"</li> * <li>"jar:/com/idrsolutions/pdf/pdfhelp/res/jpedal.pdf"</li> * </ul> */public void setFileLocations(String[] fileLocations) {/** * Add code here to parse fileLocations list, and  * add any valid locations to the File List */}}

Creating a BeanInfo Implementation

Simply having a JavaBean in its most simplistic form however, is rarely an acceptable solution. This is because you have no control over which properties are exposed to the end-user. NetBeans will simply expose all the properties in your Swing component which conform to the JavaBean architecture – whether you want all those properties exposed or not.

In the case of my example therefore, where the PdfHelpPanel extends JPanel, all the applicable fields in the PdfHelpPanel and the JPanel superclass will be exposed. From the JPanel alone this will mean there are a lot of properties accessible to the end-user.

In my case this isn't what I want. I am trying to make the PdfHelpPanel as simple as possible for developers to add to their existing Swing applications, so I want to expose just the minimum number of properties. In fact, I only want to expose one property – the fileLocations property.

To control which properties are exposed to the GUI builder and which are not, we need to associate an implementation of the BeanInfo interface with our JavaBean.

There are two primary ways of generating a BeanInfo implementation, either implement the Class “by hand” with a code editor, or by using the BeanInfo Editor provided with NetBeans. Out of the two I certainly prefer the later option. Since the release of NetBeans 6.1 the Java Beans functionality which had been dropped in NetBeans 6.0 has been reinstated with some new features. An article outlining how to use the BeanInfo Editor is provided here

As previously mentioned, I want to hide all properties other than the fileLocations. This means in my PdfHelpPanelBeanInfo the only PropertyDescriptor I want to return in my array of PropertyDescriptor from the getPropertyDescriptors() method, is the fileLocations property – which is connected to the setFileLocations setter method. The complete PdfHelpPanelBeanInfo class listing is shown below.

public class PdfHelpPanelBeanInfo extends SimpleBeanInfo {                   private static final int PROPERTY_locations = 0;private static final int defaultPropertyIndex = -1;private static final int defaultEventIndex = -1;public BeanDescriptor getBeanDescriptor() {BeanDescriptor beanDescriptor = new BeanDescriptor(PdfHelpPanel.class, null);return beanDescriptor;}public PropertyDescriptor[] getPropertyDescriptors() {PropertyDescriptor[] properties = new PropertyDescriptor[1];try {properties[PROPERTY_locations] = new PropertyDescriptor("fileLocations",PdfHelpPanel.class, null,"setFileLocations");} catch (IntrospectionException e) {e.printStackTrace();}return properties;}public EventSetDescriptor[] getEventSetDescriptors() {EventSetDescriptor[] eventSets = new EventSetDescriptor[0];return eventSets;}public MethodDescriptor[] getMethodDescriptors() {MethodDescriptor[] methods = new MethodDescriptor[0];return methods;}public int getDefaultPropertyIndex() {return defaultPropertyIndex;}public int getDefaultEventIndex() {return defaultEventIndex;}}

Building a Distributable JAR

Once you have implemented your JavaBean component, and its matching BeanInfo class, you are ready to build your JAR file. To flag up the fact that the PdfHelpPanel is a JavaBean, and allow developer applications – such as NetBeans – to pick up on that fact, you need to make an entry in the JAR's manifest file. If like me you use ANT to build your JAR files, you can very easily add the necessary information. As part of the “jar” -> “manifest” task you can mark a specific class as a JavaBean. The ANT snippet below shows how in my example I mark the PdfHelpPanel Class.

<jar destfile="${dist.dir}/${pdfhelp.jar}" index="true" basedir="${bin.dir}">  <manifest>     <attribute name="Main-Class" value="${default.class}" />     <section name="PdfHelpPanel.class">          <attribute name="Java-Bean" value="true"/>     </section>  </manifest></jar> 


This tutorial will be completed in another article tomorrow!

javabean, jpedal, netbeans, pdf, pdfhelp

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}