Java JTree Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like 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 DeclarationThe 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, AccessibleCommonly Used ConstructorsConstructor 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: 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. Comment More infoAdvertise with us Next Article TreeSet in Java S sanketnagare Follow Improve Article Tags : Java Geeks Premier League java-swing Geeks Premier League 2023 Practice Tags : Java Similar Reads B-Tree in Java A B-tree is a self-balanced tree data structure that will maintain the sorted data and allow for operations such as insertion, deletion and search operations. B-tree is particularly well-suited for systems that need to perform disk-based operations and it minimizes the number of disk accesses requir 6 min read TreeMap in Java TreeMap is a part of the Java Collection Framework. It implements the Map and NavigableMap interface and extends the AbstractMap class. It stores key-value pairs in a sorted order based on the natural ordering of keys or a custom Comparator. It uses a Red-Black Tree for efficient operations (add, re 11 min read TreeMap in Java TreeMap is a part of the Java Collection Framework. It implements the Map and NavigableMap interface and extends the AbstractMap class. It stores key-value pairs in a sorted order based on the natural ordering of keys or a custom Comparator. It uses a Red-Black Tree for efficient operations (add, re 11 min read TreeSet in Java TreeSet provides an implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like simple set with the exception that it stores elements in sorted format. Following are the features of TreeSet. TreeSet uses tree data structure for storage.Objects are stored in sorted, 3 min read TreeSet in Java TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree(red - black tree) for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equ 13 min read Segment Tree in Java A Segment Tree is a binary tree used for storing intervals or segments. It is allowed to query sum, minimum, maximum or other associative operations over the range of elements in the array efficiently. Each node in the segment tree represents the interval of an array. Leaves of the segment tree corr 5 min read Like