JaVa
   

The JTree

The JTree class is great for displaying hierarchical information, such as the directory structure of a hard drive. Let's first look at an example app showing how we can use the JTree, and then we will look into detail at the source behind the app.

Code Listing 17: Using the JTree
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.util.*;
public class JTreeExample extends JFrame implements ActionListener,
 TreeSelectionListener
{
 public static void main(String[] argv)
 {
 JTreeExample mainApp = new JTreeExample();
 }
 public JTreeExample()
 {
 super("JTree Example");
 setBounds(0, 0, 400, 350);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 rootNode = new DefaultMutableTreeNode("Root");
 treeModel = new DefaultTreeModel(rootNode);
 tree = new JTree(treeModel);
 tree.setBounds(10, 10, 370, 200);
 tree.addTreeSelectionListener(this);
 // Create a JTextField...
 textfield = new JTextField(15);
 textfield.setLocation(10, 220);
 textfield.setSize(textfield.getPreferredSize());
 // Create the button...
 addButton = new JButton("Add Node to Tree");
 addButton.setBounds(200, 220, 170, 20);
 addButton.setSize(addButton.getPreferredSize());
 // Add the action listeners...
 addButton.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(tree);
 getContentPane().add(textfield);
 getContentPane().add(addButton);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == addButton)
 {
 // Check there is text in the 'textfield'
 if(textfield.getText().compareTo("") != 0)
 {
 if(currentSelection != null)
 {
 // Add the node to the tree...
 DefaultMutableTreeNode node = new
 DefaultMutableTreeNode(textfield.getText());
 currentSelection.add(node);
 treeModel.reload();
 // Clear the textfield...
 textfield.setText("");
 }
 }
 }
 }
 public void valueChanged(TreeSelectionEvent e)
 {
 TreePath path = tree.getSelectionPath();
 if(path != null)
 {
 currentSelection = (DefaultMutableTreeNode)
 path.getLastPathComponent();
 } }
 DefaultMutableTreeNode rootNode;
 DefaultTreeModel treeModel;
 JTree tree;
 DefaultMutableTreeNode currentSelection;
 JTextField textfield;
 JButton addButton;
}



Java End example

When we execute this example app, we can see that it looks like the following:

Java Click To expand
Screenshot: Using the JTree

In our example, we can add "nodes" to our tree by entering text into the text field and clicking the Add Node to Tree button. Note though that the example will add the node to the currently selected item in the tree. Here is how the example app looks once we have added some nodes:

Java Click To expand
Screenshot: Adding nodes to our JTree in the example

Let's now look at the example code that we used to create the JTree. First we create a node that will act as the "root" for our JTree, which we then pass into the constructor for the DefaultTreeModel class that gives us a DefaultTreeModel object. Here is the code to accomplish this:

rootNode = new DefaultMutableTreeNode("Root");
treeModel = new DefaultTreeModel(rootNode);


Now that we have our model, we can use it to create our actual JTree object with the following line of code:

tree = new JTree(treeModel);


Next we set the physical bounds of our tree object and also add a TreeSelectionListener, which our main class implements. The TreeSelectionListener calls a function called valueChanged, which we are required to implement. However, let's first look at the code we used to set the bounds and add the listener:

tree.setBounds(10, 10, 370, 200);
tree.addTreeSelectionListener(this);


For storing the current value of the tree (which we require so we can add more nodes when we press the button on the app), we have a parameter called currentSelection in our main class that is of type DefaultMutableTreeNode. Don't worry too much about all the different classes required to implement the JTree; we are only scraping the surface in this tutorial, as the JTree is a very complex object and would take a lot of space to explain all its functionality. We are hoping to give a rough understanding of how to use it so that you can experiment with it yourself and, of course, learn more about it. Now back to the point. When the valueChanged method is called by the listener, we first get a TreePath by calling the getSelectionPath from our tree object. This looks like the following:

TreePath path = tree.getSelectionPath();


Once we have the path to the current selection, we can then get the actual node by calling the getLastPathComponent method from the actual path reference. This can be seen in the following line of code:

currentSelection = (DefaultMutableTreeNode)
 path.getLastPathComponent();


Every time the user changes the selection in the tree, the currentSelection object will be updated to contain the currently selected object. Let's now look at the block of code that we execute when the user clicks the button with some text in the text field.

DefaultMutableTreeNode node = new
 DefaultMutableTreeNode(textfield.getText());
currentSelection.add(node);
treeModel.reload();


First we create a new node, passing the text from our text field into the constructor. Then we add the node to the currentSelection by calling the add method of our currentSelection object (which, if you remember, is a reference to the currently selected node in the tree). Finally, we make a call to the reload method of the JTree's model to update the visual representation of the tree.

JaVa
   
Comments