Java Reference
In-Depth Information
Figure 17-19.
DynamicUtilTreeNode Hashtable tree sample
TreeModel Interface
The
TreeModel
interface describes the basic data model structure for a
JTree
. It describes a
parent-child aggregation relationship, which permits any object to be a parent or a child. There
is one root to the tree, and all other nodes of the tree are descendants of this node. In addition
to returning information about the different nodes, the model requires any implementers to
manage a list of
TreeModelListener
objects so that the listeners can be notified when any nodes
in the model have changed. The remaining method,
valueForPathChanged()
, is meant to provide
the means of changing the contents of a node at a particular location.
public interface TreeModel {
// Properties
public Object getRoot();
// Listeners
public void addTreeModelListener(TreeModelListener l);
public void removeTreeModelListener(TreeModelListener l);
// Instance methods
public Object getChild(Object parent, int index);
public int getChildCount(Object parent);
public int getIndexOfChild(Object parent, Object child);
public boolean isLeaf(Object node);
public void valueForPathChanged(TreePath path, Object newValue);
}
DefaultTreeModel Class
The
JTree
automatically creates a
DefaultTreeModel
instance to store its data model. The
DefaultTreeModel
class provides an implementation of the
TreeModel
interface that uses
TreeNode
implementations at each node.
In addition to implementing the methods of the
TreeModel
interface, as well as managing
a list of
TreeModelListener
objects, the
DefaultTreeModel
class adds several helpful methods: