Java Reference
In-Depth Information
Example 14−6: YesNoPanelBeanInfo.java (continued)
catch(IntrospectionException e) { return null; }
}
// Initialize a static array of PropertyDescriptor objects that provide
// additional information about the properties supported by the bean.
// By explicitly specifying property descriptors, we are able to provide
// simple help strings for each property; these would not be available to
// the beanbox through simple introspection. We are also able to register
// a special property editors for the messageText property
static PropertyDescriptor[] props = {
prop("messageText", "The message text that appears in the bean body"),
prop("alignment", "The alignment of the message text"),
prop("yesLabel", "The label for the Yes button"),
prop("noLabel", "The label for the No button"),
prop("cancelLabel","The label for the Cancel button"),
prop("font", "The font for the message and buttons"),
prop("background", "The background color"),
prop("foreground", "The foreground color"),
};
static {
props[0].setPropertyEditorClass(YesNoPanelMessageEditor.class);
}
/** Return the property descriptors for this bean */
public PropertyDescriptor[] getPropertyDescriptors() { return props; }
/** The message property is most often customized; make it the default */
public int getDefaultPropertyIndex() { return 0; }
}
Defining a Simple Property Editor
A bean can also provide auxiliary PropertyEditor classes for use by a beanbox
tool. PropertyEditor is a flexible interface that allows a bean to tell a beanbox
how to display and edit the values of certain types of properties.
A beanbox tool always provides simple property editors for common property
types, such as strings, numbers, fonts, and colors. If your bean has a property of a
nonstandard type, however, you should register a property editor for that type.
The easiest way to “register” a property editor is through a simple naming conven-
tion. If your type is defined by the class X , the editor for it should be defined in
the class X Editor . Alternatively, you can register a property editor by calling the
PropertyEditorManager.registerEditor() method, probably from the constructor
of your BeanInfo class. If you call this method from the bean itself, the bean then
depends on the property editor class, so the editor has to be bundled with the
bean in applications, which is not desirable. Another way to register a property
editor is by using a PropertyDescriptor object in a BeanInfo class to specify the
PropertyEditor for a specific property. The YesNoPanelBeanInfo class does this
for the messageText property, for example.
The PropertyEditor interface can seem confusing at first. Its methods allow you
to define three techniques for displaying the value of a property and two
Search WWH ::




Custom Search