Java Program to Create a JTree with a Root Node and Other Nodes Spanning from Root Node


This is a Java Program to Create a JTree with a Root Node and Other Nodes Spanning from Root Node

Problem Description

We have to write a program in Java such that it creates a tree with a root node and nodes spanning from it.

Expected Input and Output

For creating a JTree, we can have the following set of input and output.


Consider, we want to create the tree given below :

To create a tree:

On the execution of the program,
it is expected that the tree is created and displayed on the frame.
Problem Solution

1. Create a root node ‘A’.
2. Create nodes ‘B’ and ‘C’, and add them to their parent node ‘A’.
3. Create node ‘D’, and add it to its parent node ‘B’.
4. Create nodes ‘E’ and ‘F’, and add them to their parent node ‘C’.
5. Create a tree with the root node ‘A’, and add the tree to frame.
6. Display the frame.

Program/Source Code

Here is source code of the Java Program to create a tree using JTree. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /* Java Program to create a tree*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import javax.swing.JTree;
  5. import javax.swing.tree.*;
  6. class Tree
  7. {
  8.     //Driver function
  9.     public static void main(String args[])
  10.     {
  11. 	//Create a frame
  12. 	JFrame frame = new JFrame("Tree");
  13. 	frame.setSize(500,500);
  14. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 	//Create root 'A'
  16. 	DefaultMutableTreeNode A=new DefaultMutableTreeNode("A");
  17. 	//Create children 'B' & 'C'
  18. 	DefaultMutableTreeNode B=new DefaultMutableTreeNode("B");
  19. 	DefaultMutableTreeNode C=new DefaultMutableTreeNode("C");
  20. 	A.add(B);
  21. 	A.add(C);
  22. 	//Create child 'D' of B
  23. 	DefaultMutableTreeNode D=new DefaultMutableTreeNode("D");
  24. 	B.add(D);
  25. 	//Create children 'E' and 'F' of C
  26. 	DefaultMutableTreeNode E=new DefaultMutableTreeNode("E");
  27. 	DefaultMutableTreeNode F=new DefaultMutableTreeNode("F");
  28. 	C.add(E);
  29. 	C.add(F);
  30. 	//Create a tree
  31. 	JTree tree=new JTree(A);
  32. 	frame.add(tree);
  33. 	//Display the frame
  34. 	frame.setVisible(true);
  35.     }
  36. }
Program Explanation

1. To create a node use the DefaultMutuableTreeNode class.
2. To create a tree use JTree class and specify the root node of the tree.

Runtime Test Cases

Here’s the run time test case for creating a tree using JTree.


Test case – To view the tree.

Sanfoundry Global Education & Learning Series – Java Programs.

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn