How to Build a Tree in Java - dummies

By Doug Lowe

A tree is a fancy Swing component that displays hierarchical data in outline form in Java, which computer nerds refer to as a tree. The type of tree you’re probably most familiar with is the directory structure of your disk drive. Trees are created from the JTree class.

Before you can actually create a tree control, you must build the tree it displays. The easiest way to do that is to use the DefaultMutableTreeNode class.

The DefaultMutableTreeNode class implements the TreeNode interface. As a result, you can use DefaultMutableTreeNode objects for any of the methods listed in this table that call for TreeNode objects.

Constructor Description
DefaultMutableTreeNode() Creates an empty tree node.
Creates a tree node with the specified user object.
Method Description
void add(TreeNode child) Adds a child node.
TreeNode getFirstChild() Gets the first of this node’s children.
Gets the next sibling.
TreeNode getParent() Gets this node’s parent.
Object getUserObject() Gets the user object for this node.

The DefaultMutableTreeNode class provides three basic characteristics for each node:

  • The user object, which contains the data represented by the node. You can use objects of any type you want for the user object. The tree control calls the user object’s toString method to determine what text to display for each node. The easiest way to set the user object is to pass it via the DefaultMutableTreeNode constructor.

  • The parent of this node, unless the node happens to be the root.

  • Any children of this node, unless the node happens to be a leaf node. You create child nodes by calling the add method.

The DefaultMutableTreeNode class has many more methods for navigating the tree than the ones shown here. Given a root node, however, you can use the getFirstChild and getNextSibling methods to “walk” the entire tree and access each node.

The tee that is built here lists spinoff shows from three popular television shows of the past:

  • The Andy Griffith Show, which had two spinoffs: Gomer Pyle, U.S.M.C., and Mayberry R.F.D.

  • All in the Family, which directly spawned four spinoffs: The Jeffersons, Maude, Gloria, and Archie Bunker’s Place. In addition, two of these spinoffs had spinoffs of their own involving the maids: The Jeffersons’ maid became the topic of a show called Checking In, and Maude’s maid became the main character in Good Times.

  • Happy Days, which spun off Mork and Mindy, Laverne and Shirley, and Joanie Loves Chachi.

You can take many approaches to building trees, most of which involve some recursive programming. Most real programs that work with trees need some type of recursive programming to build the tree.

The first step in creating a tree is declaring a DefaultMutableTreeNode variable for each node that isn’t a leaf node. For the TV series example, let’s start with the following code:

DefaultMutableTreeModel andy, archie, happy,
      george, maude;

These variables can be local variables in the frame constructor because after you get the tree set up, you won’t need these variables anymore. You see why you don’t need variables for the leaf nodes in a moment.

Next, you create the root node:

DefaultMutableTreeNode root =
 new DefaultMutableTreeNode("Famous Spinoffs");

Now, to simplify the task of creating all the other nodes, you create a helper method called makeShow:

private DefaultMutableTreeNode makeShow(String title,
 DefaultMutableTreeNode parent)
 DefaultMutableTreeNode show;
 show = new DefaultMutableTreeNode(title);
 return show;

This method accepts a string and another node as parameters, and returns a node whose user object is set to the String parameter. The returned node is also added to the parent node as a child. Thus you can call this method to both create a new node and place the node in the tree.

The next step is creating some nodes. Continuing the example, you start with the nodes for The Andy Griffith Show and its spinoffs:

andy = makeShow("The Andy Griffith Show", root);
makeShow("Gomer Pyle, U.S.M.C.", andy);
makeShow("Mayberry R.F.D.", andy);

Here, makeShow is called to create a node for The Andy Griffith Show, with the root node specified as its parent. The node returned by this method is saved in the andy variable. Then makeShow is called twice to create the spinoff shows, this time specifying andy as the parent node.

Because neither Gomer Pyle, U.S.M.C., nor Mayberry R.F.D. had a spinoff show, you don’t have to pass these nodes as the parent parameter to the makeShow method.

Next in the example, you have to create nodes for All in the Family and its spinoffs:

archie = makeShow("All in the Family", root);
george = makeShow("The Jeffersons", archie);
makeShow("Checking In", george);
maude = makeShow("Maude", archie);
makeShow("Good Times", maude);
makeShow("Gloria", archie);
makeShow("Archie Bunker’s Place", archie);

In this case, both The Jeffersons and Maude have child nodes of their own. As a result, variables are required for these two shows so that they can be passed as the parent parameter to makeShow when you create the nodes for Checking In and Good Times.

Finally, here’s the code that creates the nodes for Happy Days and its spinoffs:

happy = makeShow("Happy Days", root);
makeShow("Mork and Mindy", happy);
makeShow("Laverne and Shirley", happy);
makeShow("Joanie Loves Chachi", happy);

The complete tree is successfully created in memory, so you can get on with the task of creating a JTree component to show off the tree.