Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up


How to Use Trees

With the JTree(in the API reference documentation) class, you can display hierarchical data. JTree doesn't actually contain your data; it's simply a view of the data. Here's a picture of a tree:

As the preceding figure shows, JTree displays its data vertically. Each row contains exactly one item of data (called a node). Every tree has a root node (called Root in the above figure) from which all nodes descend. Nodes that can't have children are called leaf nodes. In the above figure, the look-and-feel marks leaf nodes with a circle.

Non-leaf nodes can have any number of children, or even no children. In the above figure, the look-and-feel marks non-leaf nodes by drawing a folder. Typically, the user can expand and unexpand non-leaf nodes -- making their children visible or invisible -- by clicking them. By default, non-leaf nodes start out unexpanded.

When you initialize a tree, you create a TreeNode(in the API reference documentation) instance for each node in the tree, including the root. To make a node a leaf, you invoke setAllowsChildren(false) on it.

When data values change -- for example, nodes are added -- you [the data model?] can let the JTree[Model?] know there's new data by invoking one of the TreeModelListener(in the API reference documentation) methods. The JTree then displays the new data. [CHECK ALL THIS!]

Here is a picture of an application that displays a tree in a scroll pane:


Try this:
  1. Compile and run the application. The source file is TreeDemo.java.
    See Getting Started with Swing if you need help.
  2. Expand a node.
    If you're using the Basic look and feel, you do this by clicking the + sign to the left of the item.
  3. Select a node.
    If you're using the Basic look and feel, you do this by clicking the text for the node or the icon just to the left.

Below is the code from TreeDemo.java that implements the tree in the previous example.

//In initialization code in a JPanel subclass:
    //Create the nodes.
    TreeNode top = new TreeNode("The Java Series");
    TreeNode category;
    TreeNode book;

    category = new TreeNode("Books for Java Programmers");
    top.add(category);

    //Tutorial
    book = new TreeNode("The Java Tutorial: Object-Oriented Programming for the Internet");
    book.add(new TreeNode("Mary Campione"));
    book.add(new TreeNode("Kathy Walrath"));
    category.add(book);

    ...//Do the same for each book in the category...

    category = new TreeNode("Books for Java Implementers");
    top.add(category);

    //VM
    book = new TreeNode("The Java Virtual Machine Specification");
    book.add(new TreeNode("Tim Lindholm"));
    book.add(new TreeNode("Frank Yellin"));
    category.add(book);

    ...//Do the same for each book in the category...

    //Make all child-free nodes leaves.
    makeLeaves(top);

    JTree tree = new JTree(top);
. . .
//This method will be added to the TreeNode API in a future release.
protected void makeLeaves(TreeNode top) {
    Enumeration       childEnum = top.preorderEnumeration();
    TreeNode          descendant;

    while(childEnum.hasMoreElements()) {
        descendant = (TreeNode)childEnum.nextElement();
        if(descendant.getChildCount() == 0)
            descendant.setAllowsChildren(false);
    }       
}
[API discussion to follow. Discuss renderer options.]


Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up