使用 Java 在 JTree 中获取节点的兄弟节点数


使用 getSiblingCount() 方法来获取 JTree 中节点的兄弟节点数。例如,假设我们有一个节点,它不是根节点。对于这个节点,我们会找到兄弟节点数 -

node1.getSiblingCount()

以下是如何获取节点的兄弟节点数的示例 -

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);
   }
}

以下是我们 JTree 的 -

输出如下,在控制台显示结果 -

更新时间: 30-7-2019

175 次浏览

推动您的职业发展

完成课程即可获得认证

开始学习
广告