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-6. 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 also possible to have a mutable tree node that has no children, or does not allow children, and yet is not a leaf. (An empty directory with no write permissions would be an example of such a node.)

Table 17-6. TreeNode properties

Property

Data type

get

is

set

Default value

allowsChildren

boolean

·

   

childAti

TreeNode

·

   

childCount

int

·

   

leaf

boolean

 

·

  

parent

TreeNode

·

   

iindexed

     

Notice that the children are not properties ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.