The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

Trail: JavaBeans(TM)

Lesson: Bean Customization


To prepare yourself for learning about property editors and customizers, read the following documentation:

A Bean's appearance and behavior can be customized at design time within Beans-compliant builder tools. Typically there are two ways to customize a Bean:

Property Editors

A property editor is a tool for customizing a particular property type. Property editors are displayed in, or activated from property sheets. A property sheet will determine a property's type, search for a relevant property editor, and display the property's current value in a relevant way.

Property editors must implement the PropertyEditor interface. PropertyEditor provides methods that specify how a property should be displayed in a property sheet.

Here is the BeanBox's Properties sheet containing OurButton properties:

You begin the process of editing these properties by clicking on the property entry in the sheet. How each of these is displayed depends on which PropertyEditor methods you implement to return non-null (or equivalent) values.

For example, the int property editor implements the setAsText method. This indicates to the property sheet that the property can be displayed as a String, hence an editable text box will be used.

The Color and Font property editors use a separate panel, and merely use the property sheet to display the current property value. The editor is displayed by clicking on that value. To display the current property value "sample" within the property sheet you need to override isPaintable to return true, and override paintValue to paint the current property value in a rectangle in the property sheet. Here's how ColorEditor implements paintValue:

public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
    Color oldColor = gfx.getColor();
    gfx.setColor(Color.black);
    gfx.drawRect(box.x, box.y, box.width-3, box.height-3);
    gfx.setColor(color);
    gfx.fillRect(box.x+1, box.y+1, box.width-4, box.height-4);
    gfx.setColor(oldColor);
}
To support the custom property editor, you need to override two more methods: Override supportsCustomEditor to return true, and override getCustomEditor to return a custom editor instance. ColorEditor.getCustomEditor returns this.

Additionally, the PropertyEditorSupport class maintains a PropertyChangeListener list, and fires property change event notifications to those listeners when a bound property is changed.

How Property Editors are Associated with Properties

Property editors are discovered and associated with a given property by

The BDK Property Editors

The BDK provides property editors for the primitive data types like int, boolean, and float, and Color and Font class types. The source code for these property editors is in beans/apis/sun/beans/editors. These sources make a good starting point for writing your own property editors. Some things to note about the BDK property editors:

Note that if no property editor is found for a property, the BeanBox will not display that property in the Properties sheet.

Customizers

When you use a Bean Customizer, you get complete control over how to configure or edit a Bean. A Customizer is like an application that specifically targets a Bean's customization. Sometimes properties are insufficient for representing a Bean's configurable attributes. Customizers are used where sophisticated instructions would be needed to change a Bean, and where property editors are too primitive to achieve Bean customization.

All customizers must:

If a Bean that has an associated Customizer is dropped into the BeanBox, you will notice a "Customize..." item on the Edit menu.

BDK Customizers

The OurButtonCustomizer serves as an example that demonstrates the mechanics of building a customizer. OurButtonCustomizer:

The BridgeTester and JDBC Select demo Beans also have customizers.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.