Java Reference
In-Depth Information
Example 14−8: YesNoPanelMessageEditor.java (continued)
// text area to update the value as the user types and to fire the
// property change events that property editors are required to fire.
public Component getCustomEditor() {
final TextArea t = new TextArea(value);
t.setSize(300, 150); // TextArea has no preferred size, so set one
t.addTextListener(new TextListener() {
public void textValueChanged(TextEvent e) {
value = t.getText();
listeners.firePropertyChange(null, null, null);
}
});
return t;
}
// Visual display of the value, for use with the custom editor.
// Just print some instructions and hope they fit in the in the box.
// This could be more sophisticated.
public boolean isPaintable() { return true; }
public void paintValue(Graphics g, Rectangle r) {
g.setClip(r);
g.drawString("Click to edit...", r.x+5, r.y+15);
}
// Important method for code generators. Note that it really ought to
// escape any quotes or backslashes in value before returning the string.
public String getJavaInitializationString() { return "\"" + value + "\""; }
// This code uses the PropertyChangeSupport class to maintain a list of
// listeners interested in the edits we make to the value.
protected PropertyChangeSupport listeners =new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener l) {
listeners.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
listeners.removePropertyChangeListener(l);
}
}
Defining a Bean Customizer
A bean may want to provide some way for the user of a beanbox program to cus-
tomize its properties other than by setting them one at a time. A bean can do this
by creating a Customizer class for itself and registering the customizer class with
the BeanDescriptor object returned by its BeanInfo class, as in Example 14-6.
A customizer must be some kind of GUI component that is suitable for display in a
dialog box created by the beanbox. Therefore, a customizer class is typically a
subclass of Panel . In addition, a customizer must implement the Customizer inter-
face. This interface consists of methods for adding and removing property change
event listeners and a setObject() method that the beanbox calls to tell the cus-
tomizer what bean object it is customizing. Whenever the user makes a change to
the bean through the customizer, the customizer sends a PropertyChangeEvent to
any interested listeners. Finally, like a property editor, a customizer must have a
no-argument constructor, so it can easily be instantiated by a beanbox.
Search WWH ::




Custom Search