Java Reference
In-Depth Information
CellEditorListener Interface and ChangeEvent Class
Before exploring the creation of a complete TreeCellEditor , take a look at the CellEditorListener
interface definition. The interface contains two methods that are used with a CellEditor .
public interface CellEditorListener implements EventListener {
public void editingCanceled(ChangeEvent changeEvent);
public void editingStopped(ChangeEvent changeEvent);
}
The editor calls the editingCanceled() method of the registered listeners to signal that the
editing of the node's value has been aborted. The editingStopped() method is called to signal
the completion of an editing session.
Normally, it's not necessary to create a CellEditorListener . However, when creating a
TreeCellEditor (or any CellEditor ), it is necessary to manage a list of its listeners and notify
those listeners when necessary. Thankfully, this is managed for you automatically with the
help of the AbstractCellEditor .
Creating a Better Check Box Node Editor
Using the JCheckBox editor provided by the DefaultCellEditor class isn't a good option when
working with a JTree . Although the editor can be wrapped into a DefaultTreeCellEditor to get
the appropriate tree icon next to it, you can't display text within the check box (that is, besides
true or false). Other text strings can be displayed within the tree, but once a node is edited, the
text label for the edited node can only be true or false .
To have an editable check box with a text label as the tree cell editor, you must create your
own. The complete process involves creating three classes—a data model for each node of the
tree, a tree cell renderer to render this custom data structure, and the actual editor—plus a test
program to connect them all.
Note The renderer and editor created here will support check-box-like data only for editing leaf nodes. If
you want to support check boxes for nonleaf nodes, you need to pull out the code that checks for leaf nodes.
Creating the CheckBoxNode Class
The first class to be created is for the data model for each leaf node of the tree. You could use
the same data model as the JCheckBox class, but that includes extraneous information at each
node that you don't need. The only information necessary is the selected state of the node and
its text label. With the addition of some setter and getter methods for the state and label, the
class is basically defined, as shown in Listing 17-9. The other classes are not quite this easy to
formulate.
 
Search WWH ::




Custom Search