简体   繁体   中英

get All leaf of jtree

in my jtree i imported a project java that contains 2 packages and 30 classes. i would affiche all this classes from a button ,but that code diden't work perfectly ,it affiched only 22 classes (leaf).can you help me, please ^_^

btnG.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TreeModel model = tree.getModel();
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
             int packageCount = root.getChildCount();
             int  classCount=root.getLeafCount();

             for(int j = 0; j < classCount; j++){
                for(int i = 0; i < packageCount; i++){
                //------------------ package name-------------//
                String module = (String) root.getChildAt(i).getChildAt(j).toString();
                 String modulename = FilenameUtils.getBaseName(module);
                 System.out.println("----"+modulename+"***");
                //------------------ modules name-------------//
            }}
        }
    });
         int packageCount = root.getChildCount();
         int  classCount=root.getLeafCount();

         for(int j = 0; j < classCount; j++){
            for(int i = 0; i < packageCount; i++){

You can't use a predetermined value for the inner loop. Each node can have a different number of leafs. For example one package could have 10 classes and the other package 20 classes.

You need to write a generic loop that simply iterates based on the number of children in each node.

Something like:

TreeModel model = tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();

for (int i = 0; i < root.getChildCount(); i++)
{
    DefaultMutableTreeNode child = (DefaultMutableTreeNode)root.getChildAt(i);
    System.out.println(child);

    for (int j = 0; j < child.getChildCount(); j++)
    {
        System.out.println("  - " + child.getChildAt(j));
    }
}

Of course this assumes you only have two levels of nodes.

A proper solution would use recursion to iterate through all the children.

You need to swap the for loops, the outer loop should be for packages and the inner for classes and you need to check how many classes there are per package, that is set the classCount variable for each time in the package loop

int packageCount = root.getChildCount();

for(int i = 0; i < packageCount; i++){
    classCount = root.getChildAt(i).getLeafCount();
    for(int j = 0; j < classCount; j++){
        //------------------ package name-------------//
        String module = (String) root.getChildAt(i).getChildAt(j).toString();
        String modulename = FilenameUtils.getBaseName(module);
        System.out.println("----"+modulename+"***");
        //----
    }
}

Here I have assumed there are no sub-packages, if there are then one solution is to move the above code in to a separate method that gets called recursively

I think recursive implementation is more appropriate since it works for any depth of the tree. Here is the example:

public static ArrayList<DefaultMutableTreeNode> getLeafNodes(DefaultMutableTreeNode root) {
    ArrayList<DefaultMutableTreeNode> leafs = new ArrayList<>();
    JTreeTools._getLeafNodes(root, leafs);
    return leafs;
}
private static void _getLeafNodes(DefaultMutableTreeNode parent, ArrayList<DefaultMutableTreeNode> leafs) {
    Enumeration children = parent.children();
    while (children.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) children.nextElement();
        if (node.isLeaf()) {
            leafs.add(node);
        } else {
            _getLeafNodes(node, leafs);
        }
    }
}

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