Get the number of siblings of a node in JTree with Java

Use the getSiblingCount() method to get the number of siblings of a node in JTree. For example, let’s say we have a node, which isn’t a root node. For that, we will find the sibling count −
node1.getSiblingCount()
The following is an example to get the number of siblings of a 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("Project");
      DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("QA");
      DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Domain");
      DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Topics");
      DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Tools");
      node.add(node1);
      node.add(node2);
      node.add(node3);
      node.add(node4);
      DefaultMutableTreeNode one = new DefaultMutableTreeNode("Python QA");
      DefaultMutableTreeNode two = new DefaultMutableTreeNode("jQuery QA");
      DefaultMutableTreeNode three = new DefaultMutableTreeNode("Management ");
      DefaultMutableTreeNode four = new DefaultMutableTreeNode("Finance");
      DefaultMutableTreeNode five = new DefaultMutableTreeNode("Networking");
      DefaultMutableTreeNode six = new DefaultMutableTreeNode("MIS");
      DefaultMutableTreeNode seven = new DefaultMutableTreeNode("DBMS");
      DefaultMutableTreeNode eight = new DefaultMutableTreeNode("CSS");
      DefaultMutableTreeNode nine = new DefaultMutableTreeNode("MongoDB");
      DefaultMutableTreeNode ten = new DefaultMutableTreeNode("Code Formatter");
      DefaultMutableTreeNode eleven = new DefaultMutableTreeNode("XML Editor");
      DefaultMutableTreeNode twelve = new DefaultMutableTreeNode("Code Beatifier");
      DefaultMutableTreeNode thirteen = new DefaultMutableTreeNode("Internet Speed Tester");
      node1.add(one);
      node1.add(two);
      node2.add(three);
      node2.add(four);
      node2.add(five);
      node3.add(six);
      node3.add(seven);
      node3.add(eight);
      node3.add(nine);
      node4.add(ten);
      node4.add(eleven);
      node4.add(twelve);
      node4.add(thirteen);
      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 root node = " + node.getChildCount());
      System.out.println("Depth of Tree = " + node.getDepth());
      System.out.println("Count of Tree Leaves(root node) = " + node.getLeafCount());
      System.out.println("Count of Tree Leaves(node1) = " + node1.getLeafCount());
      System.out.println("Number of levels above root node = " + node.getLevel());
      System.out.println("Number of levels above node1 = " + node1.getLevel());
      System.out.println("Number of levels above node2 = " + node2.getLevel());
      System.out.println("Number of levels above node3 = " + node3.getLevel());
      System.out.println("Number of levels above node4 = " + node4.getLevel());
      System.out.println("Sibling count of root node = " + node.getSiblingCount());
      System.out.println("Sibling count of node1 = " + node1.getSiblingCount());
      System.out.println("Sibling count of child node three = " + three.getSiblingCount());
      tree.setRowHeight(20);
      frame.add(tree);
      frame.setSize(600,450);
      frame.setVisible(true);
   }
}
The following is our JTree −
The output is as follows displaying the result in Console −