Java Reference
In-Depth Information
CHAPTER 17
■ ■ ■
Trees
I
n Chapter 16, you looked at how to work with the text document capabilities within the Swing
component set. In this chapter, you'll learn how to work with Swing's tree class, the
JTree
component.
Introducing Trees
The
JTree
component is the visual component for displaying hierarchical data elements, also
known as
nodes
. Using this tree metaphor, imagine the tree is flipped upside down. The node
at the top of the tree is called the
root
. Extending from the root node of the tree are branches to
other nodes. If a node does not have any branches coming out of it, that node is called a
leaf
node
. See Figure 17-1 for a simple
JTree
.
Figure 17-1.
Sample JTree
Many interconnected classes are used in the composition of the
JTree
. First, the
JTree
implements the
Scrollable
interface, so that you can place the tree within a
JScrollPane
for scroll
management. The display of each node within the tree is controlled by implementations of the
TreeCellRenderer
interface; by default, the implementation is the
DefaultTreeCellRenderer
class. Nodes of a tree are editable with implementations of
TreeCellEditor
. Two editor implemen-
tations are available: one offering a text field with
DefaultTreeCellEditor
and one offering a check
box or combo box with
DefaultCellEditor
, which extends from the generic
AbstractCellEditor
.
If these classes don't provide what you need, you can place custom editors within an
EditorContainer
.
623