Table of contents
1.
Introduction
2.
Working of JTree in Java
3.
Constructors in JTree
4.
Methods in JTree
5.
Example of JTree in Java
6.
Frequently Asked Questions
6.1.
What is JTree in Java?
6.2.
Which class creates a node in JTree?
6.3.
What is the default mutable tree node?
6.4.
What is JTable in Java?
7.
Conclusion
Last Updated: Mar 27, 2024

JTree In Java

Author soham Medewar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The JTree class is being used to display hierarchical or tree-structured data. JTree is a sophisticated component. It has a top-most 'root node,' which serves as a parent for all nodes in the tree. It is a descendant of the JComponent class.

JTree is a concept in the Java swing methodology. It's used to show hierarchical info that's in a specific order. It also has a root node, which in the Java framework is the most significant node. In addition, wherever a hierarchy of data must be shown, the Jtree notion is employed in computer languages. There are child nodes that show the display of the child nodes as well. For each root node, there are offspring nodes. If there are no offspring nodes for a given root node, that node is referred to as the leaf node.

Working of JTree in Java

A root node, also known as the children node, can have several nodes beneath it. There are additionally JTable, JFile, and JList instances. JTable is used to display any size table, whereas JList is a dropdown list from which we may choose an item to use in our display. JFrame is also used in the JTree list, which can be used to define and construct a frame in the Java computer language.

Constructors in JTree

There are three constructors that are typically present in the Jtree class, and they are:

  • Jtree (): A function Object() constructor with the same name as the class and no return value is called Jtree. It generates a simple model for the JTree class.
  • JTree (Object value[]): An object is passed through the function Object() constructor in this scenario. All of the objects transferred are children of the root node, which is represented at a lower level.
  • Jtree (TreeNode root): The root node in this case is TreeNode, which is constructed using the commands provided. The root node TreeNode will include all of the child nodes.

Methods in JTree

The following are some of the methods:

  • Public TreeModel getModel(): The model of the tree whose data is displayed using the Jtree in Java programming language is displayed by public TreeModel getModel ().
  • Public int getRowCount(): This function is used to count the number of rows in the Jtree. The number of rows also mentions the number of child nodes that are present under the root node of the Jtree.
  • Public void addTreeSelectionListener(TreeSelectionListener): This method helps us to add a listener to the tree selection in the JTree panel.

Example of JTree in Java

Jtree is a component of the Swing approach and is derived from it. The javax.swing file is imported first, followed by the creation of the Class Example. In the software, there is a single root node and numerous child nodes. Different types of nodes can be built under a single root node.

A Jtree is a programming concept that is used to establish a hierarchy. The first appears at the top, while the last appear at the bottom. A root node that is created can have sub-nodes. This is an example of a programme in which hierarchy appears first, followed by the variables that are present. Due to the fact that Java is an object-oriented programming language, a Default Mutable Tree Node is provided before the nodes are constructed.

In the example below, we are creating India as a root node and its child node is cities. Under the cities node, there are various cities. Under each city node, it has its means of transport with its name.

Code

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class tree
{
	JFrame f;
	tree()
	{
		f=new JFrame();

		// defining country and cities node
		DefaultMutableTreeNode country = new DefaultMutableTreeNode("India");
		DefaultMutableTreeNode cities = new DefaultMutableTreeNode("Cities");

		// adding cities node to country
		country.add(cities);

		// defining cities
		DefaultMutableTreeNode bombay = new DefaultMutableTreeNode("Bombay");
		DefaultMutableTreeNode delhi = new DefaultMutableTreeNode("Delhi");
		DefaultMutableTreeNode kolkata = new DefaultMutableTreeNode("Kolkata");
		DefaultMutableTreeNode chennai = new DefaultMutableTreeNode("Chennai");
		DefaultMutableTreeNode banglore = new DefaultMutableTreeNode("Banglore");
		DefaultMutableTreeNode hyderabad = new DefaultMutableTreeNode("Hyderabad");

		// adding cities
		cities.add(bombay); 
		cities.add(delhi);
		cities.add(kolkata);
		cities.add(chennai);
		cities.add(banglore);
		cities.add(hyderabad);

		// defining public transport to each cities
		DefaultMutableTreeNode tbombay = new DefaultMutableTreeNode("Transport in Bombay");
		DefaultMutableTreeNode tdelhi = new DefaultMutableTreeNode("Transport in  Delhi");
		DefaultMutableTreeNode tkolkata = new DefaultMutableTreeNode("Transport in Kolkata");
		DefaultMutableTreeNode tchennai = new DefaultMutableTreeNode("Transport in Chennai");
		DefaultMutableTreeNode tbanglore = new DefaultMutableTreeNode("Transport in Banglore");
		DefaultMutableTreeNode thyderabad = new DefaultMutableTreeNode("Transport in Hyderabad");

		// adding public transport to each cities
		bombay.add(tbombay); 
		delhi.add(tdelhi);
		kolkata.add(tkolkata);
		chennai.add(tchennai);
		banglore.add(tbanglore);
		hyderabad.add(thyderabad); 

		// adding transport details in each city
		// for bombay city
		DefaultMutableTreeNode CSIA = new DefaultMutableTreeNode("Chhatrapati Shivaji International Airport");
		DefaultMutableTreeNode CSTRS = new DefaultMutableTreeNode("Chhatrapati Shivaji Terminus Railway Station");
		DefaultMutableTreeNode MCBS = new DefaultMutableTreeNode("Mumbai Central Bus Station");
		tbombay.add(CSIA); tbombay.add(CSTRS); tbombay.add(MCBS);

		// for delhi city
		DefaultMutableTreeNode IGIA = new DefaultMutableTreeNode("Indira Gandhi International Airport");
		DefaultMutableTreeNode HNRS = new DefaultMutableTreeNode("Hazrat Nizamuddin Railway Station");
		DefaultMutableTreeNode MPISBT = new DefaultMutableTreeNode("Maharana Pratap Inter State Bus Terminus");
		tdelhi.add(IGIA); tdelhi.add(HNRS); tdelhi.add(MPISBT);

		// for kolkata city
		DefaultMutableTreeNode NSCBIA = new DefaultMutableTreeNode("Netaji Subhas Chandra Bose International Airport");
		DefaultMutableTreeNode HRS = new DefaultMutableTreeNode("Howrah Railway Station");
		DefaultMutableTreeNode EBT = new DefaultMutableTreeNode("Esplanade Bus Terminus");
		tkolkata.add(NSCBIA); tkolkata.add(HRS); tkolkata.add(EBT);

		// for chennai city
		DefaultMutableTreeNode MIMA = new DefaultMutableTreeNode("Madras International Meenambakkam Airport");
		DefaultMutableTreeNode CCRS = new DefaultMutableTreeNode("Chennai Central Railway Station");
		DefaultMutableTreeNode CMBT = new DefaultMutableTreeNode("Chennai Mofussil Bus Terminus");
		tchennai.add(MIMA); tchennai.add(CCRS); tchennai.add(CMBT);

		// for banglore city
		DefaultMutableTreeNode KIA = new DefaultMutableTreeNode("Kempegowda International Airport");
		DefaultMutableTreeNode KSRRS = new DefaultMutableTreeNode("Krantivira Sangolli Rayanna Railway Station");
		DefaultMutableTreeNode MBT = new DefaultMutableTreeNode("Majestic Bus Terminus");
		tbanglore.add(KIA); tbanglore.add(KSRRS); tbanglore.add(MBT);

		// for hyderabad city
		DefaultMutableTreeNode RGIA = new DefaultMutableTreeNode("Rajiv Gandhi International Airport");
		DefaultMutableTreeNode HDRS = new DefaultMutableTreeNode("Hyderabad Deccan Railway Station");
		DefaultMutableTreeNode MGBS = new DefaultMutableTreeNode("Mahatma Gandhi Bus Station");
		thyderabad.add(RGIA); thyderabad.add(HDRS); thyderabad.add(MGBS); 
		///////////////

		JTree jt=new JTree(country);
		f.add(jt);
		f.setSize(200,200);
		f.setVisible(true);
	}
	public static void main(String[] args) 
	{
		new tree();
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Frequently Asked Questions

What is JTree in Java?

The JTree class is used to display hierarchical or tree-structured data. JTree is a sophisticated component. It has a top-most 'root node,' which serves as a parent for all nodes in the tree. It is a descendant of the JComponent class.

Which class creates a node in JTree?

To create a node, use the DefaultMutuableTreeNode class.

What is the default mutable tree node?

DefaultMutableTreeNode has methods for inspecting and altering a node's parent and children, as well as procedures for inspecting the tree in which the node is located.

What is JTable in Java?

The JTable is a two-dimensional cell table that may be seen and edited.

What is MVC in Swing?

Swing's model-view-controller architecture (MVC) serves as the foundation for all of its components. MVC divides user interface components into three parts. Each of these elements has a significant impact on how the component functions.

Conclusion

In this article, we have discussed the use of JTree in Java. Also, we have seen the practical implementation of the JTree.

After reading about the JTree in Java, are you not feeling excited to read/explore more articles on the topic of Java? Don't worry; Coding Ninjas has you covered. To learn, see Multilevel inheritance in JavaMultiple Inheritance in Java, and Static Binding vs Dynamic Binding.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Coding!

Live masterclass