Introduction of JTree in Java
JTree is a concept used in Java swing methodology. It is used to display hierarchical data which is in a particular order. It also has a root node which is the most important node in the Java framework. Also, the Jtree concept is used in programming languages wherever a hierarchy of data has to be displayed. There are children nodes in which the display of the children nodes are also shown. There are children nodes for every root node. However, if there is no children node for a particular root node then that node is referred to as the leaf node. An example of a Jtree implementation is shown below where there are vegetables and fruits as the root node because they get subdivided into many other nodes. The children nodes under this case are capsicum, carrot, cabbage, and potato which comes under the root node Vegetables. Also, other children nodes are banana, mango, apple and grapes which come under the root node Fruits which can also be created.
Working of JTree in Java
There can be multiple nodes under a root node also known as the children node. There are also instances of JTable, JFile, and JList. JTable is used to display a table of any size while JList gives a dropdown list from which we can select an item and use it in our display. There is also usage of JFrame in the JTree list which can be used to clarify and build a frame in the Java programming language.
Constructors
There are commonly three constructors which are present and which shows the Jtree as a class and they are as follows:
- Jtree () – A constructor has the same name as the class name and it does not have any return value. It creates a simple model for class JTree.
- JTree (Object value[]) – In this case, an object is passéd through the constructor. All the objects passed are the child of the root node which is represented at a lower level than the root node.
- Jtree (TreeNode root) – Here the root node is TreeNode which is built according to the commands given. All the child notes will fall under the root node TreeNode.
There are also methods which are shown in Jtree.
Methods
Some of the methods are as follows:
- Public TreeModel getModel ()– It displays the model of the tree whose data is displayed using the Jtree in Java programming language.
- Public int getRowCount ()– The mentioned function is used to count the number of rows in the Jtree example. 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)– Adds a listener in the tree selection in the Jtree panel.
Example of JTree in Java
First, we see a Jtree example in the coding language. The Jtree is a part of Swing methodology and it is derived from that. First of all the javax.swing file is imported and then the Class Example is created. There is a single root node in the program and there are multiple child nodes in the program. There can be different nodes that can be created under a single root node. In the following program, we show a single root node that is color and then we notice that there is a child node called state. Now under state node, there are various states which are under the node which are known as child nodes. The child nodes are added in the code very easily. There can be other states that can be added as well.
Basically a Jtree is used to create a hierarchy in the programming concept. The first one comes at the top while the last one comes at the bottom. There can be sub-nodes to a root node that is created. This example is a program where hierarchy is a priority and then comes the variables that are present. Obviously, Java being an object-oriented programming language there is a Default Mutable Tree Node that is given and then the nodes are created.
A root node without child nodes is known as a leaf node. We can also set the size of the node created. In this program, we set the size as 150 and 150 that is the height and width of the node that is being created. There is also a main () that is created which helps in the main formation of the program. There is no IO exception in this case so the import java.io.* the package is not exported. The String argument is also created in the main () which is the default.
In the following code, we see the tree. We add four states under the country India and we use it as a Jtree. The code for executing the Jtree is shown below:
Code:
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class Example {
JFrame f;
Example(){
f=new JFrame();
DefaultMutableTreeNode country=new DefaultMutableTreeNode("India");
DefaultMutableTreeNode state=new DefaultMutableTreeNode("States");
country.add(state);
DefaultMutableTreeNode wb=new DefaultMutableTreeNode("West Bengal");
DefaultMutableTreeNode del=new DefaultMutableTreeNode("Delhi");
DefaultMutableTreeNode ap=new DefaultMutableTreeNode("Andhra Pradesh");
DefaultMutableTreeNode tn=new DefaultMutableTreeNode("Tamil Nadu");
state.add(wb); state.add(del); state.add(ap); state.add(tn);
JTree jt=new JTree(state);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new Example();
}}
Output:
Conclusion
In this article, we see the functioning of Jtree and how it is used as a class and contains constructors and methods to implement the same. Jtree is used to display a hierarchy of data and it is used in many industries like telecommunications, financial, industrial applications and many more. Jtree is a part of the Java Swing methodology and is very useful for displaying the root node as well as the child node which is part of the root node. In this article, we also see a code which is used to display the hierarchy of a root node. Just like a tree has roots, the code has a lot of roots that can be used to display the child nodes as well. The first node is always the root node and it displays the child node in a very smart fashion. Hierarchical data is important in all industries and it is very different from Excel VBA or any other programming language.
Recommended Articles
This has been a guide to the JTree in Java. Here we also discuss the Introduction of JTree in Java along with its working and constructor. You may also have a look at the following articles to learn more–