Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages


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.

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.

public class TreeDemo extends JPanel {
    public TreeDemo() {
	//Create the nodes.
	DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
	DefaultMutableTreeNode category;
	DefaultMutableTreeNode book;

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

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

	//Arnold/Gosling
	book = new DefaultMutableTreeNode("The Java Programming Language");
        book.add(new DefaultMutableTreeNode("Ken Arnold"));
        book.add(new DefaultMutableTreeNode("James Gosling"));
	category.add(book);

	//FAQ
	book = new DefaultMutableTreeNode("The Java FAQ");
        book.add(new DefaultMutableTreeNode("Jonni Kanerva"));
	category.add(book);

	//Chan/Lee
	book = new DefaultMutableTreeNode("The Java Class Libraries: An Annotated Reference");
        book.add(new DefaultMutableTreeNode("Patrick Chan"));
        book.add(new DefaultMutableTreeNode("Rosanna Lee"));
	category.add(book);

	//Threads
	book = new DefaultMutableTreeNode("Concurrent Programming in Java: Design Principles and Patterns");
        book.add(new DefaultMutableTreeNode("Doug Lea"));
	category.add(book);

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

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

	//Language Spec
	book = new DefaultMutableTreeNode("The Java Language Specification");
        book.add(new DefaultMutableTreeNode("James Gosling"));
        book.add(new DefaultMutableTreeNode("Bill Joy"));
        book.add(new DefaultMutableTreeNode("Guy Steele"));
	category.add(book);

	JTree tree = new JTree(top);

        //Create the scroll pane and add the tree to it. 
	JScrollPane scrollPane = new JScrollPane(tree);

	//Add the scroll pane to this panel.
	setLayout(new GridLayout(1, 0)); 
        add(scrollPane);
    }
    . . .
}
[API discussion to follow. Discuss renderer options.]


Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages