Chapter 33. The JTree Class

Much like the JTable and JList, the JTree class consists of a data model and an Observer. One of the easiest ways to build the tree to display is to create a root node, then add child nodes to it, and then add child nodes to each of them as needed. The DefaultMutableTreeNode class is provided as an implementation of the TreeNode interface.

We create the JTree with a root node as its argument.

    root = new DefaultMutableTreeNode("Foods");
    JTree tree = new JTree(root);

Then we add each node to the root and additional nodes to those to any depth. The following program produces a food tree list by category. Note that we use the JxFrame class, derived from JFrame at the beginning of this section.

public class TreeDemo extends JxFrame {
    private DefaultMutableTreeNode root;
    public TreeDemo() {
        super("Tree Demo");
    JPanel jp = new JPanel();    //create the interior panel
    jp.setLayout(new BorderLayout());
    getContentPane().add(jp);

    //create a scrollpane
    JScrollPane sp = new JScrollPane();
    jp.add("Center", sp);

    //create a root node
    root = new DefaultMutableTreeNode("Foods");
    JTree tree = new JTree(root);    //create the tree
    sp.getViewport().add(tree);      //add to the scroller

    //create three nodes, each with three subnodes
    addNodes("Meats", "Beef", "Chicken", "Pork");
    addNodes("Veggies", "Broccoli", "Carrots", "Peas");
    addNodes("Desserts", "Charlotte Russe",
            "Bananas Flambe", "Peach Melba");


    setSize(200, 300);
    setVisible(true);
}
//--------------
    private void addNodes(String b, String n1, String n2,
                String n3) {
        DefaultMutableTreeNode base =
            (new DefaultMutableTreeNode(b));
        root.add(base);
        base.add(new DefaultMutableTreeNode(n1);
        base.add(new DefaultMutableTreeNode(n2));
        base.add(new DefaultMutableTreeNode(n3));
    }

The tree it generated is shown in Figure 33.1.

A simple JTree display.

Figure 33.1. A simple JTree display.

If you want to know if a user has clicked on a particular line of this tree, you can add a TreeSelectionListener and catch the valueChanged event. The TreePath that we can obtain from the getPath method of the TreeSelectionEvent is the complete path back to the top of the tree. However, the getLastPathComponent method will return the string of the line that the user actually selected. You will see that we use this method and display in the Composite pattern example.

public void valueChanged(TreeSelectionEvent evt)      {
TreePath path = evt.getPath();
    String selectedTerm =
        path.getLastPathComponent().toString();

The TreeModel Interface

The previous tree was based on adding a set of nodes to make up a tree. This is an implementation of the DefaultTreeModel class that handles this structure. However, you might want to display other sorts of data structures using this tree display. To do so, you create a class of your own to hold these data that implement the TreeModel interface. This interface is very relatively simple, consisting only of the following:

void addTreeModelListener(TreeModelistener 1);
Object getChild(Object parent, int index);
int getChildCount(Object parent);
int getIndexOf Child(Object parent, Object child);
Object getRoot();
boolean isLeaf(Object);
void removeTreeModelListener(TreeModelListener 1);
void value ForPathChanges(TreePath path, Object newValue);

Note that this general interface model does not specify anything about how you add new nodes or add nodes to nodes. You can implement that in any way that is appropriate for your data.

Programs on the CD-ROM

Program Description

SwingTreeTreeDemo.java

Creates a simple tree from DefaultMutable TreeNodes.

Summary

This brief chapter touched on some of the more common Swing controls and noted how often the design patterns we've discussed in this book are represented. You might be able to use them more effectively after appreciating the patterns on which they are based.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset