Java Reference
In-Depth Information
hierarchical data that describes a tree. The interface allows you to find out which node is the parent
to the current node, as well as get information about the set of child nodes. When the parent
node is null , the node is the root of a tree.
public interface TreeNode {
// Properties
public boolean getAllowsChildren();
public int getChildCount();
public boolean isLeaf();
public TreeNode getParent();
// Other methods
public Enumeration children();
public TreeNode getChildAt(int childIndex);
public int getIndex(TreeNode node);
}
Note Normally, only nonleaf nodes allow children. However, security restrictions may limit nonleaf nodes
from having children, or at least showing them. Imagine a directory tree in which you don't have read access
to a particular directory. Although the directory is a nonleaf node, it can't have child nodes because you don't
have access to find out what those children are.
MutableTreeNode Interface
Although the TreeNode interface allows you to retrieve information about a hierarchy of tree
nodes, it doesn't allow you to create the hierarchy. TreeNode just provides you access to a read-
only tree hierarchy. On the other hand, the MutableTreeNode interface allows you to create the
hierarchy and store information at a specific node within the tree.
public interface MutableTreeNode implements TreeNode {
// Properties
public void setParent(MutableTreeNode newParent);
public void setUserObject(Object object);
// Other methods
public void insert(MutableTreeNode child, int index);
public void remove(MutableTreeNode node);
public void remove(int index);
public void removeFromParent();
}
When creating the hierarchy of tree nodes, you can either create children nodes and add
them to their parent or create parent nodes and add children. To associate a node with a parent
node, you set its parent with setParent() . Using insert() allows you to add children to a parent
node. The arguments for the insert() method include an index argument. This index represents
the position within the set of children to add the child node provided. The index is zero-based,
so an index of zero will add the node as the first child of the tree. Adding a node as the last child,
 
Search WWH ::




Custom Search