JTree

JTree is a container component that displays hierarchical data in "outline form". All trees start with a root node at the top left of the control. By default, the root is visible, along with the nodes in the level immediately below it. "Container" nodes can be "opened" and "closed" by manipulating the graphical element specified by the current "look and feel". Any node can be selected by clicking on it.

JTree follows the Model-View-Controller architecture like all Swing components. But since the DefaultTreeModel class is such a reasonable implementation of the TreeModel interface, and, the TreeNode inheritance hierarchy is fundamentally the cornerstone of the DefaultTreeModel, a TreeModel object rarely needs to be architected and created.

TreeNode is the minimal interface needed to specify "read-only" nodes. MutableTreeNode is an embellished interface that adds specifications for child management operations. DefaultMutableTreeNode is an implementation of the MutableTreeNode interface and is almost always sufficient for any application. Unless the programmer requires a lightweight tree for special processing, it's not worth considering creating your own MutableTreeNode class.

The example uses the recursive method buildTree() to:

It also demonstrates how to create a "listener" object that will be notified whenever changes to the tree's nodes' labels are made. The next example demonstrates how to intercept selection, expansion, and contraction events.

[Reference: Topley, pp787-873]