Count the Child of Root Node in a JTree



In this article, we will go over a Java program that counts the number of child nodes of the root node in a JTree using Java. This program uses the getChildCount() method to retrieve and display the count of direct child nodes under the root. This is helpful for applications involving hierarchical structures, like directories or organizational charts.


Steps to count the child of the root node in a JTree

Following are the steps to count the child of the root node in a JTree ?

  • Import the necessary classes JFrame, JTree, and DefaultMutableTreeNode from java.swing package.
  • Create a main class named SwingDemo.
  • Define the root node of the JTree with a label (e.g., "Website").
  • Add child nodes to this root node for different sections like "Videos" and "Tutorials."
  • Further, add sub-nodes to these child nodes as required.
  • Initialize a JTree with the root node.
  • Use getChildCount() on the root node to count its direct children.
  • Display the result in the console.
  • Set up and display the JTree in a frame.

Java program to count the child of the root node in a JTree

The following is an example of counting the child of the root node in a JTree ?

package my;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class SwingDemo {
   public static void main(String[] args) throws Exception {
      JFrame frame = new JFrame("Demo");
      DefaultMutableTreeNode node = new DefaultMutableTreeNode("Website");
      DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Videos");
      DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Tutorials");
      DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("QA");
      DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Tools");
      node.add(node1);
      node.add(node2);
      node.add(node3);
      node.add(node4);
      DefaultMutableTreeNode one = new DefaultMutableTreeNode("PHP Videos");
      DefaultMutableTreeNode two = new DefaultMutableTreeNode("HTML5 Videos");
      DefaultMutableTreeNode three = new DefaultMutableTreeNode("Blockchain Videos");
      DefaultMutableTreeNode four = new DefaultMutableTreeNode("Java");
      DefaultMutableTreeNode five = new DefaultMutableTreeNode("DBMS");
      DefaultMutableTreeNode six = new DefaultMutableTreeNode("CSS");
      DefaultMutableTreeNode seven = new DefaultMutableTreeNode("MongoDB");
      DefaultMutableTreeNode eight = new DefaultMutableTreeNode("Python QA");
      DefaultMutableTreeNode nine = new DefaultMutableTreeNode("jQuery QA");
      DefaultMutableTreeNode ten = new DefaultMutableTreeNode("Photo Editing Tool");
      node1.add(one);
      node1.add(two);
      node1.add(three);
      node2.add(four);
      node2.add(five);
      node2.add(six);
      node2.add(seven);
      node3.add(eight);
      node3.add(nine);
      node4.add(ten);
      JTree tree = new JTree(node);
      for (int i = 0; i < tree.getRowCount(); i++) {
          tree.expandRow(i);
      }
      tree.putClientProperty("JTree.lineStyle", "Angled");
      System.out.println("Number of children of node = " + node.getChildCount());
      tree.setRowHeight(25); frame.add(tree);
      frame.setSize(600,450);
      frame.setVisible(true);
   }
}

Output

The output is as follows. The Console displays the count of node ?

The folowing is the JTree ?

Code explanation

In this code, we define a root node named "Website" and add four main child nodes to it: "Videos", "Tutorials", "QA", and "Tools". Each of these child nodes has its nested nodes, forming a hierarchical tree structure. The getChildCount() method is then used on the root node to count its direct children, which are displayed in the console as output. Additionally, the JTree structure is visually displayed in a frame, showing the tree and expanded nodes.

Updated on: 2024-11-13T19:20:01+05:30

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements