- The Tree Control
- Tree Appearance
- The TreeNode Interface
- The MutableTreeNode Interface
- The DefaultMutableTreeNode Class
- The TreePath Class
- What is a Leaf?
- Tree Expansion and Traversal
- Expanding and Collapsing Nodes under Program Control
- Tree Expansion Events
- Making Nodes Visible
- Controlling Node Expansion and Collapse
- Tree Model Events
- Implementation Plan for the File System Control
- File System Tree Control Implementation
- Using the File System Tree Control
- Custom Tree Rendering and Editing
- Customizing the Default Tree Cell Renderer
- ToolTips and Renderers
- Custom Cell Editors
- Controlling Which Nodes Can Be Edited
- Controlling Editability by Subclassing JTree
- Programmatic Control of Editors
- Editing Trees with Custom User Objects
- The valueForPathChanged Method
- The Tree Implementation
- The Cell Editor
- The Cell Renderer
- Summary
The Cell Editor
Constructing the cell editor is also a simple matterall we need is an editor that displays a combo box containing the list of hardware items that can appear in the tree. The combo box itself can be created by calling the Tree-Model's getNamesAsCombo method. Therefore, creating and installing a suitable editor requires only four lines of code:
// Define a replacement editor JComboBox combo = m.getNamesAsCombo(); DefaultCellEditor comboEditor = new DefaultCellEditor(combo); DefaultTreeCellEditor editor = new DefaultTreeCellEditor(t, r, comboEditor); t.setCellEditor(editor);
The basic editor is created by wrapping the combo with a standard DefaultCellEditor, which is then wrapped in a DefaultTreeCellEdi-tor to make it behave suitably within a JTree. In this code extract, the variable r refers to the custom cell renderer for the tree that will be discussed next. Even though the editing process requires a certain amount of clever handling to arrange for the String from the combo box to be converted to the correct HardwareObject to be installed in the node, none of this complication is visible to the editor because it is all implemented in the Tree-Model. Therefore, it is perfectly acceptable to use a standard editor.