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.

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 the properties of its parent.

Creating Tree in Java

Step 1: Import the necessary package.
In Java it provides a spate package with in swing named as tree.
  1. import javax.swing.*;
  2. import javax.swing.tree.*;
Step 2: Create the DefaultMutableTreeNode objects as required.
  1. DefaultMutableTreeNode c = new DefaultMutableTreeNode("Collage", true);
  2. DefaultMutableTreeNode d = new DefaultMutableTreeNode("Department");
  3. DefaultMutableTreeNode f = new DefaultMutableTreeNode("Faculty");
  4. DefaultMutableTreeNode a = new DefaultMutableTreeNode("Acoountant");
  5. DefaultMutableTreeNode r = new DefaultMutableTreeNode("Resgistrar");
  6. DefaultMutableTreeNode m = new DefaultMutableTreeNode("Managenent");
  7. DefaultMutableTreeNode s = new DefaultMutableTreeNode("Security");
Step 3: Now add all the nodes according your requirements for making a parent-child relation.
  1. c.add(d);
  2. d.add(f);
  3. d.add(a);
  4. d.add(m);
  5. m.add(s );
  6. c.add(a);
Step 4: Create an object of the JTree class by passing the parent node (c) in JTree.
  1. JTree mytree = new JTree(c);
Step 5: At last you create a frame and add the tree into the frame.
  1. JFrame frame = new JFrame("Demo of JTree Component!");
  2. frame.add(mytree);
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  4. frame.setUndecorated(true);
  5. frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  6. frame.setSize(450,450);
  7. frame.setVisible(true);
Step 6: Now you can combine the code and run your Jtree Demo program.
Complete code
  1. import javax.swing.*;
  2. import javax.swing.tree.*;
  3. public class TreeComponent
  4. {
  5. public static void main(String[] args)
  6. {
  7. DefaultMutableTreeNode c = new DefaultMutableTreeNode("Collage", true);
  8. DefaultMutableTreeNode d = new DefaultMutableTreeNode("Department");
  9. DefaultMutableTreeNode f = new DefaultMutableTreeNode("Faculty");
  10. DefaultMutableTreeNode a = new DefaultMutableTreeNode("Acoountant");
  11. DefaultMutableTreeNode r = new DefaultMutableTreeNode("Resgistrar");
  12. DefaultMutableTreeNode m = new DefaultMutableTreeNode("Managenent");
  13. DefaultMutableTreeNode s = new DefaultMutableTreeNode("Security");
  14. c.add(d);
  15. d.add(f);
  16. d.add(a);
  17. d.add(m);
  18. m.add(s);
  19. c.add(a);
  20. JTree mytree = new JTree(c);
  21. JFrame frame = new JFrame("Demo of JTree Component!");
  22. frame.add(mytree);
  23. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. frame.setUndecorated(true);
  25. frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  26. frame.setSize(450, 450);
  27. frame.setVisible(true);
  28. }
  29. }
Output
cmd output
Tree in Java
Initial output
Tree in Java
After Expanding the tree
Tree in Java
Resources