Java Reference
In-Depth Information
Root
Edge
Node
Parent
Children
Leaf
(a)
(b)
Figure 16.6 Tw o graphical representations of the same tree: (a) mathematical and (b) used
in graphical interfaces
defined in class JTree which displays the abstract information of the tree model.
The individual nodes of the tree are defined as DefaultMutableTreeNode . These
are then inserted into the tree model together with the structural information,
i.e. the information that tells you which node is a child of which other node. We
describe these three classes in the following sections and then show two examples
of applications using trees.
16.4.1
The class DefaultMutableTreeNode
Class DefaultMutableTreeNode is a convenience implementation of the interface
TreeNode . For our applications, the following methods are sufficient:
DefaultMutableTreeNode(Object label)
add(DefaultMutableTreeNode node)
DefaultMutableTreeNode(Object label) defines a mutable tree node. The
node receives object label as label. When a node is displayed one sees a sym-
bol and the label to the right of the symbol. The label is often a string. The term
'mutable' refers to the fact that clicking on such a node in the display makes
the subtree under this node appear or disappear. This function is automatically
supplied by DefaultMutableTreeNode .
add(DefaultMutableTreeNode node) adds node node as the right-most child to
this node. We shall not use this method in our examples but it can be helpful
in some situations.
When nodes are displayed, they receive a default symbol. For DefaultMutable-
TreeNode this is the 'folder' symbol of Java if the node is not a leaf. If the node is
a leaf the 'file' symbol of Java is displayed.
Search WWH ::




Custom Search