Property Editor Examples

The JavaBeans specification and VisualAge support the following types of editors implementing the java.beans.PropertyEditor interface:

Tag-based. This editor presents a fixed list of property values (known individually as tags) from a drop-down list in the property sheet.

Text-based. This editor accepts a single string from within a property sheet, parsing it as necessary to set the property.

Custom. This editor opens a window separate from the property sheet to collect settings information.

Paintable. This editor paints a graphic representation of the property value back into the property sheet rather than returning a String value.

Each type of property editor supports a subset of the PropertyEditor interface, meaning that a different set of method implementations returns non-null values. A summary follows:

For properties that require special code to be generated for initialization, the getJavaInitializationString( ) method should return a non-null value. VisualAge uses this value when it generates code for the bean whose property you are setting. (For an example, see Custom Editor for the Person Bean.)

For examples, look at the com.ibm.ivj.examples.vc.propertyeditors package shipped with VisualAge in the IBM Java Examples project. Consider the Person bean, which has the following properties: name, address, phoneNumber, sex, and incomeRange.

public class Person {
   String fieldSex = "";
   protected transient java.beans.PropertyChangeSupport propertyChange
      = new java.beans.PropertyChangeSupport(this);
   transient Address fieldAddress = null;
   static public final int belowTwenty = 1;
   static public final int twentyToFifty = 2;
   static public final int fiftyToOneHundred = 3;
   static public final int aboveOneHundred = 4;
   int fieldIncomeRange = 0;
   String fieldPhoneNumber = "";
   Name fieldName = null;
}

Because possible incomeRange values are predefinable, this property can be set using a tag-based editor. The phoneNumber property has a known format in each locale, so the example uses a text-based editor and validates the input argument in setAsText( ) to make sure it matches the North American notion of a telephone number. For the name property, the example uses a custom editor instead of setAsText( ); name is an instance of a serializable class, as required. We illustrate a paintable editor for the sex property. Because this example does not include an editor for the address property and the Address class happens not to be serializable, the instance has been marked as transient.

The sample package illustrates two means of associating a property editor. The sex, incomeRange, and phoneNumber properties are of types (String and int) for which there is default editor support in VisualAge, so editors for these properties are explicitly assigned in the bean's BeanInfo class. The name property is of type Name; because a NameEditor class exists in the same package, Java uses it by default.

For a tour of each property editor, follow the Related Reference links below. To test these editors, we used two simple visual composites based on Applet, PersonTester and PaintTester. When you double-click on the Person1 bean in each composite, a standard property sheet appears with certain property editor examples enabled.


Related procedures
Enabling Custom Edit Support for Your Bean

Related references
Tag-Based Editor for the Person Bean
Text-Based Editor for the Person Bean
Custom Editor for the Person Bean
Paintable Editor for the Person Bean