Working With Tree in Java


Introduction

This article describes working with a Tree structure in Java. The Tree is a very common component and I think all of you have seen them in many applications. A control that displays a set of hierarchical data as an outline. You can find task-oriented documentation and examples of using trees in Ebooks content.
A specific node in a tree can be identified either by a TreePath (an object that encapsulates a node and all of its ancestors), or by its display row, where each row in the display area displays one node. An expanded node is a non-leaf node (as identified by TreeModel.isLeaf(node) returning false) that will display its children when all its ancestors are expanded. A collapsed node is one which hides them. A hidden node is one which is under a collapsed ancestor. All of a viewable nodes parents are expanded, but may or may not be displayed. A displayed node is both viewable and in the display area, where it can be seen.  

Following are components Descriptions of the tree.

JTree :
The tree is a graphical representation of data with the hierarchical properties by adding nodes to nodes and keeps the relation as parent and child.

Node : A node is an object and is available at any position within the JTree where the data associated is represented.
 
Path : The path is a collection of a contiguous set of nodes that contains one or many nodes. The path is empty or null when the path does not have a node.

Leaf :
This is a special type of node at the end of a path. A leaf node is not connected to other nodes.

Root :
This is the node at the highest point within the hierarchy in a tree.
 
Parent : 
It represents the relationship of node with another node. In object-oriented programming concepts the parent is a super class.

Child :
It represents the relationship of node with another node. In object-oriented programming concepts the child is a subclass of its parent. It inherits all properties of its parent.

For Creating the Tree you have to use the following steps.

Step 1 : Import the necessary package.

In Java it provide a spate package with in swing named as tree.
import javax.swing.*;
import javax.swing.tree.*;

Step 2 : Create the DefaultMutableTreeNode objects as required.

DefaultMutableTreeNode c = new DefaultMutableTreeNode("Collage", true);

DefaultMutableTreeNode d = new DefaultMutableTreeNode("Department");

DefaultMutableTreeNode f = new DefaultMutableTreeNode("Faculty");

DefaultMutableTreeNode a = new DefaultMutableTreeNode("Acoountant");

DefaultMutableTreeNode r = new DefaultMutableTreeNode("Resgistrar");

DefaultMutableTreeNode m = new DefaultMutableTreeNode("Managenent");

DefaultMutableTreeNode s = new DefaultMutableTreeNode("Security");

Step 3 : Now add all the nodes according your requirements for making a parent-child relation.

c.add(d);

d.add(f);

d.add(a);

d.add(m);

m.add(s );

c.add(a);

Step 4 : Create an object of the JTree class by passing the parent node (c) in JTree.

JTree mytree = new JTree(c);
 

Step 5 : At last you create a frame and add the tree into the frame.

JFrame frame = new JFrame("Demo of JTree Component!");

frame.add(mytree);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setUndecorated(true);

frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

frame.setSize(450,450);

frame.setVisible(true);

Step 6 : Now you can combine the code and run your Jtree Demo program.

Complete code

import javax.swing.*;

import javax.swing.tree.*;

public class TreeComponent

{

public static void main(String[] args)

{

DefaultMutableTreeNode c = new DefaultMutableTreeNode("Collage", true);

DefaultMutableTreeNode d = new DefaultMutableTreeNode("Department");

DefaultMutableTreeNode f = new DefaultMutableTreeNode("Faculty");

DefaultMutableTreeNode a = new DefaultMutableTreeNode("Acoountant");

DefaultMutableTreeNode r = new DefaultMutableTreeNode("Resgistrar");

DefaultMutableTreeNode m = new DefaultMutableTreeNode("Managenent");

DefaultMutableTreeNode s = new DefaultMutableTreeNode("Security");

c.add(d);

d.add(f);

d.add(a);

d.add(m);

m.add(s );

c.add(a);

JTree mytree = new JTree(c);

JFrame frame = new JFrame("Demo of JTree Component!");

frame.add(mytree);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setUndecorated(true);

frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

frame.setSize(450,450);

frame.setVisible(true);

}

}

Output:

cmd output

tree03.jpg

Initial output

tree01.jpg

After Expanding the tree

tree02.jpg

Resources

Describing the Java Packages
Use of ByteStreams and CharacterStreams in JAVA
Garbage Collector in .NET
Describing the JSF Elements

Up Next
    Ebook Download
    View all
    Learn
    View all