Java Reference
In-Depth Information
if(selected) {
leafRenderer.setForeground(selectionForeground);
leafRenderer.setBackground(selectionBackground);
} else {
leafRenderer.setForeground(textForeground);
leafRenderer.setBackground(textBackground);
}
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode)value).getUserObject();
if (userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode)userObject;
leafRenderer.setText(node.getText());
leafRenderer.setSelected(node.isSelected());
}
}
returnValue = leafRenderer;
} else {
returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
Note The getLeafRenderer() method is a helper method you'll need in the editor.
Creating the CheckBoxNodeEditor Class
The CheckBoxNodeEditor class is the last part of creating a better check box node editor. It serves
as the TreeCellEditor implementation, allowing you to support editing of trees whose leaf
node data is of type CheckBoxNode . The TreeCellEditor interface is an extension of the CellEditor
implementation, so you must implement the methods of both interfaces. You can't extend
DefaultCellEditor or DefaultTreeCellEditor , because they would require you to use the
JCheckBox editor implementation they provide, instead of the new one you're creating here.
However, you can extend AbstractCellEditor and add the necessary TreeCellEditor interface
implementation. An AbstractCellEditor manages the list of CellEditorListener objects for
you and has methods to notify the list of listeners upon either stopping or canceling editing.
Because the editor acts as the renderer, you'll need to use the earlier
CheckBoxNodeRenderer to get the basic renderer appearance. This will ensure that the editor
appears similar to the renderer. Because the renderer for the leaf nodes will be a JCheckBox , this
works perfectly well to enable you to change the node state. The editor JCheckBox will be active
and changeable, allowing a user to change from a selected state to an unselected state, and vice
versa. If instead the editor were the standard DefaultTreeCellRenderer , you would need to
manage the creation of selection changes.
 
Search WWH ::




Custom Search