- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to count the child of root node in a JTree
Use the getChildCount() method to count the number of root node in a JTree. Here, our root node is “node”. Therefore to count its child node, use −
System.out.println("Number of children of node = " + node.getChildCount());
We have displayed the count in the Console as shown above. The following is an example to count the child of root node in a JTree −
Example
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 −
- Related Articles
- Java Program to get the count of child nodes of any node in JTree
- How to get the depth of root node in a JTree with Java?
- Java Program to get the previous node from a JTree
- Check whether a node is a root node or not in JTree
- Child node count in JavaScript?
- Get the number of siblings of a node in JTree with Java
- I want to return the next node after this node in a JTree with Java
- How to find a node in a JTree Component with Java?
- Get the number of levels above a node in JTree with Java
- How to get this node’s last child in a JTree with Java?
- How to get this node’s first child in a JTree with Java?
- How to get the leaf after this node in a JTree component with Java?
- How to get the number of siblings of this node in a JTree?
- Can I get the node at a specified index in a JTree with Java?
- How to implement the mouse right-click on each node of JTree in Java?\n

Advertisements