Java Reference
In-Depth Information
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Running the program and selecting a CheckBoxNode will enable the editor. After the editor
is enabled, selecting the editor again causes the state of the node within the tree to change. The
editor stays enabled until a different tree node is selected. Figure 17-16 shows an example of
the editor in use.
Figure 17-16. The new CheckBoxNodeEditor in action
Working with the Nodes of the Tree
When you create a JTree , the type of objects at any spot in the tree can be any Object . There
is no requirement that the nodes of the tree implement any interface or subclass any class.
Nevertheless, the Swing component libraries provide a pair of interfaces and one class for
working with tree nodes. The default data model for the tree, DefaultTreeModel , uses these
interfaces and the class. However, the tree data model interface, TreeModel , permits any type
of object to be a tree node.
The base interface for nodes is TreeNode , which defines a series of methods describing a read-
only, parent-children aggregation relationship. Expanding on TreeNode is the MutableTreeNode
interface, which allows you to programmatically connect nodes and store information at each
node. The class that implements the two interfaces is the DefaultMutableTreeNode class.
Besides implementing the methods of the two interfaces, the class provides a set of methods
for traversing the tree and inquiring about the state of various nodes.
Keep in mind that although these node objects are available, much work can be still
performed without involving these interfaces and classes, as previously shown in this chapter.
TreeNode Interface
The TreeNode interface describes one possible definition for an individual part of a tree. It's used
by one implementation of TreeModel , the DefaultTreeModel class, to store references to the
 
Search WWH ::




Custom Search