Java Reference
In-Depth Information
Now that the class hierarchy has been set up, the first method to examine is the
public
Object getCellEditorValue()
method of
CellEditor
. The purpose of this method is to convert
the data as stored within the node editor into the data as stored within the node. The user interface
calls this method to get the editor's value after it has determined that the user has successfully
changed the data within the editor. In this method, you need to create a new object each time.
Otherwise, the same node will be in the tree multiple times, causing all nodes to be equal to the
renderer for the last edited node. To convert the editor to the data model, it's necessary to ask
the editor what its current label and selected state are, and then create and return a new node.
public Object getCellEditorValue() {
JCheckBox checkbox = renderer.getLeafRenderer();
CheckBoxNode checkBoxNode =
new CheckBoxNode(checkbox.getText(), checkbox.isSelected());
return checkBoxNode;
}
■
Note
It's not the job of the editor to directly access the node within the tree to update it. The
getCellEditorValue()
method returns the appropriate node object so that the user interface can
notify the tree of any changes.
If you were to implement the
CellEditor
interface yourself, you would also need to manage
the list of
CellEditorListener
objects yourself. You would need to manage the list with the
addCellEditorListener()
and
removeCellEditorListener()
methods, and provide methods that
notify the list of listeners for each method in the interface. But, because you'll be subclassing
AbstractCellEditor
, it's not necessary to do this yourself. You just need to know that the class
provides
fireEditingCanceled()
and
fireEditingStopped()
methods in order to notify the
listener list at the appropriate times.
The next
CellEditor
method,
cancelCellEditing()
, is called when a new node of the tree
is selected, announcing that the editing process of the prior selection has stopped and any
interim update has been aborted. The method is capable of doing anything, such as destroying
any necessary interim objects used by the editor. However, what the method should do is call
fireEditingCanceled()
; this ensures that any registered
CellEditorListener
objects are notified of
the cancellation. The
AbstractCellEditor
does this for you. Unless you need to do some interim
operations, it's not necessary to override this behavior.
The
stopCellEditing()
method of the
CellEditor
interface returns a
boolean
. This method is
called to see if editing of the current node can stop. If any validation needs to be done to deter-
mine whether editing can stop, you would check here. For the
CheckBoxNodeEditor
in this
example, no validation check is necessary. Therefore, editing can always stop, allowing the
method to always return
true
.
You would call the
fireEditingStopped()
method when you want to have the editor stop
editing. For instance, if the editor were a text field, pressing Enter within the text field could act
as the signal to stop editing. In the case of the
JCheckBox
editor, selection could act as a signal
to stop the editor. If
fireEditingStopped()
isn't called, the tree data model isn't updated.
To stop editing after selection of the
JCheckBox
, attach an
ItemListener
to it.