Java Reference
In-Depth Information
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
};
editor.addItemListener(itemListener);
The next method of the CellEditor interface to look at is public boolean
isCellEditable(EventObject event) . The method returns a boolean to state whether the node
at the source of the event is editable. To find out if the event happens at a particular node, you
need a reference to the tree where the editor is to be used. You can add this requirement to the
constructor of the editor.
To find out which node is at a specific position during an event, you can ask the tree for the
path of nodes to the event location. The path is returned as a TreePath object, which is examined
again later in this chapter in the “TreePath Class” section. The last component of the tree path
is the specific node where the event happened. It is this node that you must check to determine
if it's editable. If it is editable, the method returns true ; if it isn't editable, false is returned. In
the case of the tree to be created here, a node is editable if it's a leaf node and it contains
CheckBoxNode data.
JTree tree;
public CheckBoxNodeEditor(JTree tree) {
this.tree = tree;
}
public boolean isCellEditable(EventObject event) {
boolean returnValue = false;
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent)event;
TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
if (path != null) {
Object node = path.getLastPathComponent();
if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)node;
Object userObject = treeNode.getUserObject();
returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
}
}
}
return returnValue;
}
The shouldSelectCell() method of the CellEditor interface allows you to decide whether
a node is selectable. For the editor in this example, all editable cells should be selectable. However,
Search WWH ::




Custom Search