Open In App

Java JTree

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The JTree is a type of GUI(Graphic User Interface) that displays information in a hierarchical way. This intricate component part provides a quite elegant substance of representing relationships among elements in a tree-like structure. In this exploration, we’ll delve into the essence of the JTree class, examining its declaration, normally used constructors and examples.

JTree Class Declaration

The JTree class is an extension of the JComponent class, inheriting its capabilities. It also implements the Scrollable and Accessible interfaces, enhancing its functionality and accessibility.

public class JTree extends JComponent implements Scrollable, Accessible

Commonly Used Constructors

Constructor

Description

JTree()

This constructor creates a JTree with a sample model. It serves as a quick way to initialize a tree structure without specifying a custom model.

JTree(Object[] value)

JTree is created with each element of the specified array becoming a child of a new root node. This constructor is useful when you want to build a tree structure based on an array of values.

JTree(TreeNode root)

JTree is created with the specified TreeNode as its root. This allows you to define a custom structure for your tree by providing a root node explicitly.

Example of Java JTree:

Java




// Java Program to demonstrate
// Java JTree
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
  
public class DynamicTreeExample {
    public static void main(String[] args)
    {
        // Creating the frame
        JFrame frame = new JFrame(
            "GeeksforGeeks - Java JTree Example");
  
        // Creating the root node
        DefaultMutableTreeNode root
            = new DefaultMutableTreeNode("Root");
  
        // Creating child nodes
        DefaultMutableTreeNode parent1
            = new DefaultMutableTreeNode("Parent 1");
        DefaultMutableTreeNode child1_1
            = new DefaultMutableTreeNode("Child 1.1");
        DefaultMutableTreeNode child1_2
            = new DefaultMutableTreeNode("Child 1.2");
  
        // Adding child nodes to the parent1
        parent1.add(child1_1);
        parent1.add(child1_2);
  
        // Creating another set of child nodes
        DefaultMutableTreeNode parent2
            = new DefaultMutableTreeNode("Parent 2");
        DefaultMutableTreeNode child2_1
            = new DefaultMutableTreeNode("Child 2.1");
        DefaultMutableTreeNode child2_2
            = new DefaultMutableTreeNode("Child 2.2");
  
        // Adding child nodes to the parent2
        parent2.add(child2_1);
        parent2.add(child2_2);
  
        // Adding parent nodes to the root
        root.add(parent1);
        root.add(parent2);
  
        // Creating the JTree
        JTree tree = new JTree(root);
  
        // Adding the JTree to the frame within a scroll
        // pane
        frame.add(new JScrollPane(tree));
  
        // Setting frame properties
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(
            JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


Output:

Java JTree

Mastering JTree allows Java developers to integrate hierarchical information into graphical user interfaces. Experiment with unique constructors and customization options to unlock the potential of this Java GUI component.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads