Subpages: 1. JTree
2. Basic JTree example
3. Directories tree: part I - Dynamic node retrieval
4. Directories tree: part II - Popup menus and programmatic navigation
5. Directories tree: part III - Tooltips
6. JTree and XML documents
7. Custom editors and renderers
17.2 Basic JTree example - network object IDs
As we know very well by now, JTree is suitable for the display and editing of a hierarchical set of objects. To demonstrate this in an introductory-level example, we will consider a set of Object Identifiers (OIDs) used in the Simple Network Management Protocol (SNMP). In the following example, we show how to build a simple JTree displaying the initial portion of the OID tree.
SNMP is used extensively to manage network components, and is particularly important in managing internet routers and hosts. Every object managed by SNMP must have a unique OID. An OID is built from a sequence of numbers separated by periods. Objects are organized heirarchicaly and have an OID with a sequence of numbers equal in length to their level (see 17.1.1) in the OID tree. The International Organization of Standards (ISO) establishes rules for building OIDs.
Note that understanding SNMP is certainly not necessary to understand this example. The purpose is to show how to construct a tree using:
A DefaultTreeModel with DefaultMutableTreeNodes containing custom user objects.
A customized DefaultTreeCellRenerer.
A TreeSelectionListener which displays information in a status bar based on the TreePath encapsulated in the TreeSelectionEvents it receives.

Figure 17.2 JTree with custom cell renderer icons, selection listener, and visible root handles.
<<file figure17-2.gif>>
The Code: Tree1.java
see \Chapter17\1
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class Tree1 extends JFrame
{
protected JTree m_tree = null;
protected DefaultTreeModel m_model = null;
protected JTextField m_display;
public Tree1() {
super("Sample Tree [OID]");
setSize(400, 300);
Object[] nodes = new Object[5];
DefaultMutableTreeNode top = new DefaultMutableTreeNode(
new OidNode(1, "ISO"));
DefaultMutableTreeNode parent = top;
nodes[0] = top;
DefaultMutableTreeNode node = new DefaultMutableTreeNode(
new OidNode(0, "standard"));
parent.add(node);
node = new DefaultMutableTreeNode(new OidNode(2,
"member-body"));
parent.add(node);
node = new DefaultMutableTreeNode(new OidNode(3, "org"));
parent.add(node);
parent = node;
nodes[1] = parent;
node = new DefaultMutableTreeNode(new OidNode(6, "dod"));
parent.add(node);
parent = node;
nodes[2] = parent;
node = new DefaultMutableTreeNode(new OidNode(1, "internet"));
parent.add(node);
parent = node;
nodes[3] = parent;
node = new DefaultMutableTreeNode(new OidNode(1, "directory"));
parent.add(node);
node = new DefaultMutableTreeNode(new OidNode(2, "mgmt"));
parent.add(node);
nodes[4] = node;
node.add(new DefaultMutableTreeNode(new OidNode(1, "mib-2")));
node = new DefaultMutableTreeNode(new OidNode(3,
"experimental"));
parent.add(node);
node = new DefaultMutableTreeNode(new OidNode(4, "private"));
node.add(new DefaultMutableTreeNode(new OidNode(1,
"enterprises")));
parent.add(node);
node = new DefaultMutableTreeNode(new OidNode(5, "security"));
parent.add(node);
node = new DefaultMutableTreeNode(new OidNode(6, "snmpV2"));
parent.add(node);
node = new DefaultMutableTreeNode(new OidNode(7,
"mail"));
parent.add(node);
m_model = new DefaultTreeModel(top);
m_tree = new JTree(m_model);
DefaultTreeCellRenderer renderer = new
DefaultTreeCellRenderer();
renderer.setOpenIcon(new ImageIcon("opened.gif"));
renderer.setClosedIcon(new ImageIcon("closed.gif"));
renderer.setLeafIcon(new ImageIcon("leaf.gif"));
m_tree.setCellRenderer(renderer);
m_tree.setShowsRootHandles(true);
m_tree.setEditable(false);
TreePath path = new TreePath(nodes);
m_tree.setSelectionPath(path);
m_tree.addTreeSelectionListener(new
OidSelectionListener());
JScrollPane s = new JScrollPane();
s.getViewport().add(m_tree);
getContentPane().add(s, BorderLayout.CENTER);
m_display = new JTextField();
m_display.setEditable(false);
getContentPane().add(m_display, BorderLayout.SOUTH);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new Tree1();
}
class OidSelectionListener implements TreeSelectionListener
{
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getPath();
Object[] nodes = path.getPath();
String oid = "";
for (int k=0; k<nodes.length; k++) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)nodes[k];
OidNode nd = (OidNode)node.getUserObject();
oid += "."+nd.getId();
}
m_display.setText(oid);
}
}
}
class OidNode
{
protected int m_id;
protected String m_name;
public OidNode(int id, String name) {
m_id = id;
m_name = name;
}
public int getId() { return m_id; }
public String getName() { return m_name; }
public String toString() { return m_name; }
}
Understanding the Code
Class Tree1
This class extends JFrame to implement the frame container for our JTree. Three instance variables are declared:
JTree m_tree: our OID tree.
DefaultTreeModel m_model: tree model to manage data.
JTextField m_display: used as a status bar to display the selected object's OID.
The constructor first initializes the parent frame object. Then a number of DefaultMutableTreeNodes encapsulating OidNodes (see below) are created. These objects form a hierarchical structure with DefaultMutableTreeNode top at the root. Note that during the construction of these nodes, the Object[] nodes array is populated with a path of nodes leading to the "mgmt" node.
DefaultTreeModel m_model is created with the top node as the root, and JTree m_tree is created to manage this model. Then some specific options are set for this tree component. First, we replace the default icons for opened, closed and leaf icons with our custom icons, using a DefaultTreeCellRenderer as our tree's cell renderer:
DefaultTreeCellRenderer renderer = new
DefaultTreeCellRenderer();
renderer.setOpenIcon(new ImageIcon("opened.gif"));
renderer.setClosedIcon(new ImageIcon("closed.gif"));
renderer.setLeafIcon(new ImageIcon("leaf.gif"));
m_tree.setCellRenderer(renderer);
Then we set the showsRootHandles property to true, the Editable property to false, and select the path determined by the nodes array formed above:
m_tree.setShowsRootHandles(true);
m_tree.setEditable(false);
TreePath path = new TreePath(nodes);
m_tree.setSelectionPath(path);
Our custom OidSelectionListener (see below) TreeSelectionListener is added to the tree to receive notification when our tree's selection changes.
A JScrollPane is created to provide scrolling capabilities, and our tree is added to its JViewport. This JScrollPane is then added to the center of our frame. A non-editable JTextField m_display is created and added to the south region of our frame's content pane to display the currently selected OID.
Class Tree1.OidSelectionListener
This inner class implements the TreeSelectionListener interface to receive notifications about when our tree's selection changes. Our valueChanged() implementation extracts TreePath for the current selection and visits each node, starting from the root, accumulating the OID in .N.N.N form as it goes (where N is a digit). This method ends by displaying the resulting OID in our text field status bar.
Class OidNode
This class encapsulates a single object identifier as a number and a String name describing the associated object. Both values are passed to the OidNode constructor. Instances of this class are passed directly to the DefaultMutableTreeNode constructor to act as a node's user object. The overridden toString() method is used to return the name String so that our tree's cell renderer will display each node correctly. Recall that, by default, DefaultTreeCellRenderer will call a node's user object toString() method for rendering.
Running the Code
Figure 17.1 shows our OID tree in action. Try selecting various tree nodes and note how the selected OID is displayed at the bottom of the frame.
UI Guideline : Icons & Root Handles
In this example, we are visually re-inforcing the data hierarchy with icons. The icons are overloading on the root handles to communicate whether an element is a document or a container and whether that container is open or closed. The book icon has two variants to communicate "open book" and "closed book". The icons are communicating the same information as the root handles.
Therefore, it is technically possible to remove the root handles. In some problem domains, hidden root handles may be more appropriate, providing that the Users are comfortable with interpreting the book icons and realise that a "closed book" icon means that the node can be expanded.



RSS feed Java FAQ News