Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Reflecting, Introspecting, and Customizing JavaBeans

By Laura Bennett
2003-10-09


Customization using property sheets and editors

Customization gives users the ability to visually modify a bean's properties to meet their needs.

After you design your beans so that reflection and introspection can occur, you should test your code to verify that it actually works. The ability to customize on the user level is a vital part of the bean anatomy. For example, imagine you have a clock part. A user should have the ability to customize it with such properties as format (digital or analog) or time zone. A bean like this should use design properties or a BeanInfo class to allow these properties to be retrieved and set.

Visual builders use property sheets to allow users to make property changes. A property sheet displays a list of the properties associated with a bean. An example of a property sheet is shown in Figure 1.

Figure 1. Property sheet for an animation bean
An animation bean

This property sheet comes from the AppletDesigner visual builder tool in the IBM WebSphere Studio tool suite.

AppletDesigner allows you to create applets, applications, and JavaBeans by placing beans on a visual layout and connecting them to create events. Properties are listed in the first column and values are listed in the second column. You can modify the property values by selecting the second column. Depending on the property you select, either a drop-down list or a separate window appears.

Each property listed should have a property editor associated with it. A property editor is a class that allows you to edit the property of a bean visually. Java provides property editors for primitive data types such as Boolean and Color. You need to code property editors for more complicated properties such as the first picture property listed in the property sheet above. A class called FileNameEditor was developed to handle modification to the first picture property.

All property editors must implement the java.beans.PropertyEditor interface. The easiest way to create a simple PropertyEditor is to extend the java.beans.PropertyEditorSupport class that implements java.beans.PropertyEditor.

Look at a portion of the FileNameEditor class code. This sample code generates the Figure 2.

Figure 2. File name property editor
Generated from FileNameEditor class code

To test the properties of the bean that you just designed, add the bean to the AppletDesigner tool by adding your bean to the tool's Palette. To add the bean to the Palette:

  1. Select Add from the Palette pop-up menu. A dialog appears where you enter the name of the jar file that contains the JavaBean.
  2. Select the category you want to add the bean to.
  3. Click Add to add the bean.

In AppletDesigner, you select beans from the palette and drop them on the Composer window. The Composer window is used to design applets or applications consisting of JavaBeans. Select the bean you just added and drop it on the Composer window. You should see the properties displayed in the property sheet. If your properties do not appear in the property sheet or if the correct values do not appear on the right side, check how you coded your BeanInfo or your property editor. If everything appears accurately, users can customize your bean to their liking.



Tutorial Pages:
» Reflecting, introspecting, and customizing JavaBeans
» Reflection
» Customization using property sheets and editors
» Customization using customizers
» Summary
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT