Chapter 17. Trees
One
crucial component that found its way into the Swing set is the tree.
Tree components help you visualize hierarchical information and make
traversal and manipulation of that information much more manageable.
A tree consists of
nodes,
which can contain either a user-defined object along with references
to other nodes, or a user-defined object only. (Nodes with no
references to other nodes are commonly called
leaves.) In modern windowing environments, the
directory list is an excellent example of a tree. The top of the
component is the root directory or drive, and under
that is a list of
subdirectories.
If the subdirectories contain further subdirectories, you can look at
those as well. The actual files found in any directory in this
component are the leaves of the tree.
Any data with parent-child relationships can be displayed as a tree.
Another common example is an organizational chart. In such a chart,
every management position is a node, with child nodes representing
the employees under the manager. The organizational
chart's leaves are the employees who are not in
management positions, and its root is the president or CEO. Of
course, real organizations don't always adhere to a
strict tree structure. In a tree, each node has exactly one parent
node, with the exception of the root node, which cannot have a parent
(so trees are not cyclic). This means—in the world of
trees—that two managers cannot manage the same employee.
In short, whenever you have a clearly defined hierarchy, you can
express that hierarchy as a tree. Swing implements trees
with the JTree class and its related models. With
trees, as with tables, it's particularly important
to understand the models. The JTree itself merely
coordinates the tree's display.
|