Writing Advanced Beans |
To get the most out of this section, read the following documentation:
- Section 7.2 of the JavaBeans API Specification.
- IndexedPropertyDescriptor class
- PropertyEditor interface
- PropertyEditorSupport class
Indexed properties represent collections of values accessed, like an array, by index. The indexed property design patterns are
Conforming to these patterns lets builder tools know that your Bean contains an indexed property.//Methods to access the entire indexed property array public <PropertyType>[] get(); public void set<PropertyName>( [] value); //Methods to access individual values public <PropertyType> get (int index); public void set<PropertyName>(int index, value); The
OurListBox
demo Bean illustrates how to use an indexed property. OurListBox extends theList
class to provide a Bean that presents the user a list of choices: Choices that you can provide and change at design time. Here's an illustration of anOurListBox
instance:
OurListBox
exposes theitem
indexed property with the following accessor methods:public void setItems(String[] indexprop) { String[] oldValue=fieldIndexprop; fieldIndexprop=indexprop; populateListBox(); support.firePropertyChange("items",oldValue, indexprop); } public void setItems(int index, String indexprop) { String[] oldValue=fieldIndexprop; fieldIndexprop[index]=indexprop; populateListBox(); support.firePropertyChange("Items",oldValue, fieldIndexprop); } public String[] getItems() { return fieldIndexprop; } public String getItems(int index) { return getItems()[index]; }When an item is set by one of the
setItems()
methods,OurListBox
is populated with the contents of aString
array.Indexed properties are almost as easily exposed as simple properties. Writing an indexed property editor, though, requires writing a custom property editor.
Indexed Property Editors
The
OurListBox
demo Bean provides an associatedIndexPropertyEditor
which is a good example of how to implement an indexed property editor. The following illustration shows anOurListBox
instance in the BeanBox, the Properties sheet which contains an entry for the indexed propertyitems
, and theIndexPropertyEditor
which pops up when theitems
property entry is clicked:
Implementing
IndexPropertyEditor
is the same as implementing any custom property editor:
- Implement the
PropertyEditor
interface:public class IndexPropertyEditor extends Panel implements PropertyEditor, Action Listener {You can use thePropertyEditorSupport
class, either by subclassing or as an inner class.- Denote the custom editor in a related BeanInfo class.
OurListBox
has a relatedOurListBoxBeanInfo
class that contains the following code:itemsprop.setPropertyEditorClass(IndexPropertyEditor.class);
- Make the property editor a source for bound property events. The property editor will register property listeners, and fire property change events at those listeners. This is how the property changes are propagated back to the Bean (via the property sheet). So
IndexPropertyEditor
instantiates an innerPropertyChangeSupport
class:private PropertyChangeSupport support = new PropertyChangeSupport(this);Provides the ability for objects to register their interest in being notified when a property is edited:public void addPropertyChangeListener(PropertyChangeListener l) { support.addPropertyChangeListener(l); } public void removePropertyChangeListener(PropertyChangeListener l) { support.removePropertyChangeListener(l); }And fires property change events at those listeners:public void actionPerformed(ActionEvent evt) { if (evt.getSource() == addButton) { listBox.addItem(textBox.getText()); textBox.setText(""); support.firePropertyChange("", null, null); } else if (evt.getSource()== textBox) { listBox.addItem(textBox.getText()); textBox.setText(""); support.firePropertyChange("",null,null); } ... }
IndexPropertyEditor
maintainslistbox
as a proxy for OurListBox. When a change is made tolistbox
, a property change event is fired to all listeners.When the Properties sheet, which is registered as an
IndexPropertyEditor
listener, receives a property change event fromIndexPropertyEditor
, the Properties sheet callsIndexPropertyEditor.getValue()
to retrieve the new or changed items and update the Bean.
Writing Advanced Beans |