Home > Articles > Programming > Java

This chapter is from the book

This chapter is from the book

Tree Expansion and Traversal

When a tree is first created, unless you take special action, one level of nodes is visible. Nodes that have children are rendered with an expansion handle, which you can use to make their child nodes visible. When you click on the handle, the next level of nodes is drawn and the icon changes its appearance to indicate that a second click will reverse the process and collapse the node. Another way to expand or collapse a subtree is to double-click on the node itself instead of clicking the expansion box. In Java 2 version 1.3, you can change the number of clicks that are needed to expand or collapse a node by clicking on the node itself:

public void setToggleClickCount(int clickCount); 

It is also possible to navigate the tree using the keyboard. To see how this works, run the TreeExample1 program that was shown earlier:

java JFCBook.Chapter10.TreeExample1 

When this starts, you'll see the root node and two child nodes labeled Apollo and Skylab. Provided you don't click the mouse, none of these items will be highlighted. Now press the right-arrow key on your keyboard and the root node will be highlighted, indicating that it has been selected. Pressing the down arrow now moves the selection to the Apollo node and repeating it carries the selection down to Skylab. Similarly, the up arrow key moves the selection back up.

Now use the up and down arrow keys to highlight Apollo and then press the right arrow. This causes the Apollo node to expand and display its child nodes. With these nodes exposed, the down key now moves you not to Sky-lab, but to 11, the first child node of Apollo. In fact, the up and down keys just move the selection up and down by one row on the screen. If you leave the selection on 11 and press the right arrow key, this node opens to expose the names of the crew of Apollo 11. With these nodes visible, press the left arrow key. This collapses the subtree rooted at 11, but leaves 11 selected. So it appears that the left and right arrows just expand and collapse a node's subtree. However, if you press the right arrow with the selection on 11 to expand its child nodes, then press it again, the selection moves to the first child. In other words, the left and right arrow keys open and close a subtree if they can, but if the subtree is already open or closed, these keys behave like the up and down arrows, respectively.

You can also use the HOME, END, PAGE DOWN and PAGE UP keys to navigate a tree. The HOME key moves the selection back to the root, or whichever node is at the top of the tree if the root node is hidden, while END moves it to the last row of the tree. PAGE DOWN and PAGE UP scroll the tree down or up by a page. To see this, expand the tree completely and, if necessary, resize the window until the vertical scrollbar appears so that you have more in the view-port than can be displayed on one page, then press PAGE DOWN.

  • + Share This
  • 🔖 Save To Your Account