Java Reference
In-Depth Information
this method allows you to look at a specific node to see if it can be selected. By default,
AbstractCellEditor returns true for this method.
The remaining method, getTreeCellEditorComponent() , is from the TreeCellEditor inter-
face. You'll need a reference to a CheckBoxNodeRenderer to get and use that as the editor. There
are two minor changes besides just passing through all the arguments. Editors should always
be selected and have the input focus. This simply forces two arguments to always be true .
When the node is selected, the background is filled in. When focused, a border surrounds the
editor when UIManager.get("Tree.drawsFocusBorderAroundIcon") reports true .
CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row) {
// Editor always selected / focused
return renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf,
row, true);
}
Listing 17-11 shows everything put together, presenting the complete CheckBoxNodeEditor
class source.
Listing 17-11. Custom Node Editor for a JTree
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
public class CheckBoxNodeEditor extends AbstractCellEditor implements
TreeCellEditor {
CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
ChangeEvent changeEvent = null;
JTree tree;
public CheckBoxNodeEditor(JTree tree) {
this.tree = tree;
}
public Object getCellEditorValue() {
JCheckBox checkbox = renderer.getLeafRenderer();
CheckBoxNode checkBoxNode =
new CheckBoxNode(checkbox.getText(), checkbox.isSelected());
return checkBoxNode;
}
Search WWH ::




Custom Search