JTree (continued)

The tree in this example is built using a recursive function that limits the depth of the tree to 4 levels, and, makes odd-numbered "interior" nodes the root of a subtree.

The previous example demonstrated "listening" for changes to the TreeModel. This example demonstrates how to intercept selection, expansion, and contraction events. A TreeSelectionListener object provides the capability to listen for selection events, and a TreeExpansionListener object does the same for expansion and contraction events. Both TreeSelectionEvent and TreeExpansionEvent represent the name of the current node as a TreePath object.

"If DefaultMutableTreeNode is the physical representation of a node, then TreePath is its logical representation. Instead of pointing you directly at the node, it tells you the node's 'address' within the tree." [Topley, p803]   The TreePath class contains an array of type Object that is the "fully qualified path" to the node it describes. The elements of the array are often Strings, but they could be of any arbitrary type.

The function expandPath() demonstrates one of potentially many ways of exercising TreePath's interface to create a representation of the node's complete path.

The JDK documentation defines the following vocabulary. An expanded node is one that displays its children. A collapsed node is one which hides them. A hidden node is one which is under a collapsed parent. A viewable node is under an expanded parent, but may not be in the viewport's display area. A displayed node is both viewable and in the display area, where it can be seen.

[Reference: Topley, pp787-873]