Home > Articles > Programming > Java

This chapter is from the book

This chapter is from the book

Expanding and Collapsing Nodes under Program Control

JTree provides a set of methods that you can use to expand or collapse parts of the tree and to find out whether a given node is expanded or not. These methods are listed in Table 10-2.

Table 10–2 Expanding and Collapsing Nodes in a Tree



public void expandRow (int row)

Expands the node currently on the given row of the screen.

public void collapseRow(int row)

Collapses the node on the given row.

public void expandPath(TreePath path)

Expands the node with the given TreePath.

public void collapsePath(TreePath path)

Collapses the node with the given TreePath.

public boolean isExpanded(int row)

Determines whether the node on the given row is expanded.

public boolean isCollapsed (int row)

Determines whether the node on the given row is collapsed.

public boolean isExpanded(TreePath path)

Determines whether the node with the given TreePathis expanded.

public boolean isCollapsed (TreePath path)

Determines whether the node with the given TreePathis collapsed.

The isExpanded and isCollapsed methods just return a boolean that indicates whether the node is currently expanded or collapsed. You can address the node either using its screen row (which is rarely likely to be a useful option), or its TreePath, which is more useful because it is independent of how much of the tree is expanded at any given time. Notice, however, that there is no method that can be used with a TreeNode, because these are JTree methods and JTree doesn't deal with TreeNodes, except in its constructor. However, if you have a DefaultMutableTreeNode, you can get the corresponding TreePath using the following code:

// This only works if "node" is a DefaultMutableTreeNode. 
DefaultMutableTreeNode node = ....... // Initialize to point
                                // to a node in the tree 

TreeNode[] pathToRoot = node.getPath(); 
TreePath path = new TreePath(pathToRoot); 

Given a node, the DefaultMutableTreeNode getPath method returns the ordered set of nodes that lead to it from the root of the tree. For example, in the case of TreeExample1, the ordered set of nodes that would be returned by getPath for the node corresponding to Neil Armstrong would be:

Neil Armstrong 

Using a set of nodes, you can create a TreePath by using the constructor that takes an array of objects—as you saw earlier, the objects that make up a TreePath are actually just the TreeNodes in the path that it represents. This is a general method for mapping from a node to a TreePath, but it only works with a DefaultMutableTreeNode because neither the TreeNode nor the MutableTreeNode interface requires the implementation of the getPath method that was used to get the array of nodes.

The collapseRow and collapsePath methods perform the obvious functions. Again, you must know either a row number or a TreePath to use these methods. Nothing happens if the node is already collapsed.

To expand a node, you use either expandRow or expandPath. If the node has any children and it is not already expanded, its immediate children are displayed. If you want to expand more of the tree, you will need to apply the expand methods to subsequent levels. If the node that is the target of these methods is not itself visible when asked to expand, the parts of the tree that lead to it will first be expanded to make it visible. For example, if the example tree were completely closed and you asked for the node 11 to be expanded, the root node would be expanded to show Apollo and Skylab, then Apollo would be expanded to show all of the mission numbers, including 11, and finally 11 would expand to show Neil Armstrong, Buzz Aldrin, and Michael Collins. In this case, expanding a node and then collapsing it does not leave the tree in the same state as it started, because the nodes above 11 would not be collapsed. Note, however, that you can only expand or collapse a child that is not a leaf node. Therefore, if you want to expand the tree to show the node for Neil Armstrong, you would have to call expandPath on the parent of that node, not on the node itself. When a node is expanded, the tree is automatically scrolled to bring as many as possible of the node's children into view (assuming, of course, that it is mounted in a JScrollPane). If you don't want this behavior, you can disable it using the first of the following JTree methods:

public void setScrollsOnExpand(boolean cond); 
public boolean getScrollsOnExpand(); 
  • + Share This
  • 🔖 Save To Your Account