简体   繁体   中英

How can i get the count of leaf nodes of all parent nodes in Jtree Java

I am new to Jtree and Java. I have a tree structure like this :

-Abcd
 --Efghi
  ---Pqrst
  ---Uvwxyz
  ---Xyza
  ---Hdwik
  ---Lmnop
  ---Bcdef
 --Tqrsp
  ---Jumak
   ----Uoaha
   ----Lobte 
    -----Cshnt
   ----Karke 

Now i want to get the count of Abcd = 14 (ie Count of all children of Abcd+1) similarly, Abcd - Efghi = 7 (ie Count of all leafNodes of Efghi+1)

But I am not able to get the count. Here's the code :

import java.util.Enumeration;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import java.io.*;
import java.util.*;

public class treeTest {
    public treeTest(List<String> somelist) {

        DefaultMutableTreeNode root = new DefaultMutableTreeNode(somelist.get(0));


        DefaultTreeModel model = new DefaultTreeModel(root);


        JTree tree = new JTree(model);


        for(int i = 1;i<somelist.size();i++)
        {
        buildTreeFromString(model, somelist.get(i));
        }


        // UI


        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(tree);
        f.setSize(300, 300);
        f.setLocation(200, 200);
        f.setVisible(true);

        for (int i = 0; i < tree.getRowCount(); i++) {
        tree.expandRow(i);
        }

        DefaultMutableTreeNode rootNode  = ((DefaultMutableTreeNode)tree.getModel().getRoot());

       int n = tree.getModel().getChildCount(rootNode);
        System.out.println(n);


    }



    private void buildTreeFromString(final DefaultTreeModel model, final String str) {
        // Fetch the root node
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();

        // Split the string around the delimiter
        String [] strings = str.split(" - ");

        // Create a node object to use for traversing down the tree as it 
        // is being created
        DefaultMutableTreeNode node = root;

        // Iterate of the string array
        for (String s: strings) {
            // Look for the index of a node at the current level that
            // has a value equal to the current string
            int index = childIndex(node, s);

            // Index less than 0, this is a new node not currently present on the tree
            if (index < 0) {
                // Add the new node
                DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);
                node.insert(newChild, node.getChildCount());
                node = newChild;
            }
            // Else, existing node, skip to the next string
            else {
                node = (DefaultMutableTreeNode) node.getChildAt(index);
            }
        }
    }


    private int childIndex(final DefaultMutableTreeNode node, final String childValue) {
        Enumeration<DefaultMutableTreeNode> children = node.children();
        DefaultMutableTreeNode child = null;
        int index = -1;

        while (children.hasMoreElements() && index < 0) {
            child = children.nextElement();

            if (child.getUserObject() != null && childValue.equals(child.getUserObject())) {
                index = node.getIndex(child);
            }
        }

        return index;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {

          List<String> list = new ArrayList<String>();
          BufferedReader reader = new BufferedReader(new FileReader("Filepath\Sample.txt"));
          String line;
          while ((line = reader.readLine()) != null) {
          list.add(line);
        }
          reader.close();

        new treeTest(list);
    }
}

Is there any way that i could get leafcount of every parent in the tree or is there any other way to get that information without using tree?

You could get your count in one of two ways:

  1. While you are constructing the JTree nodes, you could accumulate the count you are seeking
  2. Another approach would be to traverse the tree, and then using the getChildCount() method of the node instances, you could get a count of the child nodes

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM