简体   繁体   中英

how to have Folder Structure with DefaultMutableTreeNode,JTree

I am trying to show the same folder structure of folder/subfolder as parent/children

Suppose of this Structure: 在此处输入图片说明

however I am struggling on how to add children to the parent node, I have a parent folder named Computerscience, I get all its subfolders and add them as children, how ever some of these children can be parents when there are more subfolders inside them, it seems I need a recursive function but I am not able to do it, here is what I did:

import java.awt.BorderLayout;
import java.io.File;
import java.util.Arrays;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TestTree extends JFrame {

    JTree tree;
    DefaultTreeModel treeModel;

    public TestTree() {
        super("Tree Test Example");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    protected String[] getListFiles(String Path) {
        File file = new File(Path);
        String[] names = file.list();
        return names;
    }

    public void init() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Computerscience");
        String[] names = this.getListFiles("C:\\Users\\neginn\\Google Drive\\NetBeansProjects\\computerscience");
        for (String name : names) {
            root.add(new DefaultMutableTreeNode(name));
            String[] names_level_2 = this.getListFiles("C:\\Users\\neginn\\Google Drive\\NetBeansProjects\\computerscience\\" + name);
            if (names_level_2 != null) {
                for (String name_level2 : names_level_2) {
                    int folder_index = Arrays.asList(names_level_2).indexOf(name_level2);
                    treeModel.insertNodeInto(root, (DefaultMutableTreeNode) root.getChildAt(folder_index), folder_index);
                }
            }
        }
        treeModel = new DefaultTreeModel(root);
        tree = new JTree(treeModel);

        getContentPane().add(tree, BorderLayout.CENTER);
    }

    public static void main(String args[]) {
        TestTree tt = new TestTree();
        tt.init();
        tt.setVisible(true);
    }
}

my problem is in the line treeModel.insertNodeInto

As you say, you need to write a recursive method. I tried to write one, please test code below:

public class TestTree extends JFrame {

    JTree tree;
    DefaultTreeModel treeModel;

    public TestTree() {
        super("Tree Test Example");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    protected File[] getListFiles(String Path) {
        File file = new File(Path);
        return file.listFiles();
    }

    private void addChilds(DefaultMutableTreeNode rootNode, String path) {
        File[] files = this.getListFiles(path);
        for(File file:files) {
            if(file.isDirectory()) {
                DefaultMutableTreeNode subDirectory = new DefaultMutableTreeNode(file.getName());
                addChilds(subDirectory, file.getAbsolutePath());
                rootNode.add(subDirectory);
            } else {
                rootNode.add(new DefaultMutableTreeNode(file.getName()));
            }
        }
    }

    public void init() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Computerscience");

        addChilds(root, "C:\\Users\\neginn\\Google Drive\\NetBeansProjects\\computerscience");

        treeModel = new DefaultTreeModel(root);
        tree = new JTree(treeModel);

        getContentPane().add(tree, BorderLayout.CENTER);
    }

    public static void main(String args[]) {
        TestTree tt = new TestTree();
        tt.init();
        tt.setVisible(true);
    }
}

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