Tree Nodes and Paths

You probably noticed that the DefaultTreeModel class depends on TreeNode and TreePath objects. In a tree, a TreeNode represents an individual piece of data stored at a particular point in a tree, and a path represents a collection of these pieces that are directly related to each other (in an ancestor/descendant relationship). Let’s look at the classes that make up the typical nodes and paths.

The TreeNode Interface

A TreeNode is the basic unit of a tree. This interface defines the minimum properties and access routines a typical tree model expects to see in its nodes.

Properties

The TreeNode interface contains the properties listed in Table 17.3. The TreeNode properties are straightforward and deal with the structure of the node. The parent property holds a valid value for every node in a tree, except the root. The childAt property lets you access a particular child in the tree. The childCount property contains the number of children associated with this node, if it allows children. If the node does not allow children, it is probably a leaf, but it is possible to have a mutable tree node that has no children, does not allow children, and yet is not a leaf. (An empty directory with no write permissions in a filesystem would be an example of such a node.)

Table 17-3. TreeNode Properties

Property

Data Type

get

is

set

bound

Default Value

allowsChildren

boolean

•

    

childAt (indexed)

TreeNode

•

    

childCount

int

• ...

Get Java Swing now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.