如何使用DefaultMutableTreeNode,JTree获得文件夹结构

时间:2016-03-24 20:30:05

标签: java tree jstree

我正在尝试将文件夹/子文件夹的文件夹结构显示为父/子

假设这个结构: enter image description here

但是我正在努力如何将子节点添加到父节点,我有一个名为Computerscience的父文件夹,我得到它的所有子文件夹并将它们添加为子节点,当有更多子文件夹时,这些子节点中的一些如何可以成为父节点在他们内部,似乎我需要一个递归函数,但我无法做到,这就是我所做的:

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

我的问题在于line TreeModel.insertNodeInto

1 个答案:

答案 0 :(得分:0)

正如您所说,您需要编写一个递归方法。我试着写一个,请测试下面的代码:

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