
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Count of Child Nodes of Any Node in JTree
In this program, we will create a tree structure using JTree and DefaultMutableTreeNode in Java to represent a hierarchical structure, such as a website. We will then use the getChildCount() method to retrieve and display the number of child nodes for specific nodes in the tree. The example focuses on nodes that are not root nodes and prints the count of child nodes for each.
Let's say we want the count of child nodes for node 1, which is not a root node ?
node1.getChildCount()
Steps to get the count of child nodes of any node in JTree
Following are the steps to get the count of child nodes of any node in JTree ?
- First, we will import the necessary classes: JFrame, JTree, and DefaultMutableTreeNode from the javax.swing and javax.swing.tree packages.
- Create a JFrame to hold and display the tree structure.
- Create a root node using DefaultMutableTreeNode and name it "Website."
- Create additional nodes like "Videos", "Tutorials", "QA," and "Tools," which represent different sections.
- Add these nodes as children of the root node.
- For each of these nodes, add further child nodes to represent more detailed sections (e.g., "PHP Videos," "Java").
- Create a JTree using the root node to display the hierarchy and expand all rows in the tree so that all nodes and child nodes are visible.
- Use the getChildCount() method to retrieve and print the number of child nodes for specific nodes (e.g., "Videos," "Tutorials").
- Add the tree to the frame, set its size, and make it visible to display the tree structure with the child counts.
Java program to get the count of child nodes of any node in JTree
The following is an example to get the count of child nodes of any node ?
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 node1 (Videos) = " + node1.getChildCount()); System.out.println("Number of children of node2 (Tutorials) = " + node2.getChildCount()); System.out.println("Number of children of node3 (QA) = " + node3.getChildCount()); System.out.println("Number of children of node4 (Tools) = " + node4.getChildCount()); tree.setRowHeight(25); frame.add(tree); frame.setSize(600,450); frame.setVisible(true); } }
Output
The Console displays the count of each node ?
The JTree is as follows
Code Explanation
The above program begins by creating a JFrame and a root node called "Website" using DefaultMutableTreeNode. Several child nodes are added under this root node, including "Videos," "Tutorials," "QA," and "Tools." Further child nodes are added under these nodes, such as "PHP Videos," "Java," and "Python QA." The JTree is created using the root node, and the getChildCount() method is used to print the number of children for the "Videos," "Tutorials," "QA," and "Tools" nodes. Finally, the tree is added to the frame, and the GUI is displayed with the tree structure.