Java Reference
In-Depth Information
it with the
PropertyDescriptor
for the message property, as shown in Example
14-6.
Example 14-8 shows the definition of this property editor. This is a more complex
editor that supports the creation of a custom editor component and graphical dis-
play of the value. Note that this example implements
PropertyEditor
directly,
which means that it must handle registration and notification of
PropertyChange-
Listener
objects.
getCustomEditor()
returns an editor component for multiline
strings. Figure 14-2 shows this custom editor placed within a dialog box created by
the
beanbox
program. Note that the Done button in this figure is part of the
bean-
box
dialog, not part of the property editor itself.
Figure 14−2. A custom property editor dialog
The
paintValue()
method displays the value of the
messageText
property. This
multiline value doesn't typically fit in the small rectangle of screen space allowed
for the property, so
paintValue()
displays instructions for popping up the custom
editor, which allows the user to inspect and edit the property value.
Example 14−8: YesNoPanelMessageEditor.java
package com.davidflanagan.examples.beans;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
/**
* This class is a custom editor for the messageText property of the
* YesNoPanel bean. It is necessary because the default editor for
* properties of type String does not allow multi-line strings
* to be entered.
*/
public class YesNoPanelMessageEditor implements PropertyEditor {
protected String value; // The value we will be editing.
public void setValue(Object o) { value = (String) o; }
public Object getValue() { return value; }
public void setAsText(String s) { value = s; }
public String getAsText() { return value; }
public String[] getTags() { return null; } // not enumerated; no tags
// Say that we allow custom editing.
public boolean supportsCustomEditor() { return true; }
// Return the custom editor. This just creates and returns a TextArea
// to edit the multi-line text. But it also registers a listener on the