Swing - 从JFileChooser中选择的JTree多个根文件夹

时间:2018-04-25 09:05:33

标签: java swing jtree

我是Swings的初学者。试图在JTree上构建一个小应用程序。坚持这个问题。

我可以使用JFileChooser将所选文件夹加载到树中,但如果我再打开一个文件夹,则会将新文件夹替换为上一个文件夹。有什么办法可以在树上打开多个文件夹吗?当我继续从文件选择器打开文件夹时,它必须继续添加到现有树。请建议如何实现这个目标?

以下是我尝试的工作示例(从其他帖子中选取):

import javax.swing.*;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;

public class TreeFrame {
    public static void main(String[] args) {

        JFrame frame = createFrame();

        JPanel browsePanel = new JPanel();

        JButton open = new JButton();
        open.setPreferredSize(new Dimension(70, 25));
        open.setText("Open");

        final JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        JTree tree = new JTree();
        JScrollPane scrollPane = new JScrollPane(tree);

        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                int result = fileChooser.showOpenDialog(frame);
                if (result == JFileChooser.APPROVE_OPTION) {

                    File file = fileChooser.getSelectedFile();

                    TreeModel model = new FileTreeModel(file);
                    tree.setModel(model);

                    scrollPane.add(tree);
                }
            }
        };

        open.addActionListener(action);
        browsePanel.add(open);

        frame.add(browsePanel, BorderLayout.WEST);
        frame.add(scrollPane, BorderLayout.EAST);

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static JFrame createFrame() {
        JFrame frame = new JFrame("JTree Expand/Collapse example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(500, 400));
        frame.setLayout(new GridLayout(1, 2));
        return frame;
    }

    static class FileTreeModel implements TreeModel {
        protected File root;
        public FileTreeModel(File root) { this.root = root; }

        public Object getRoot() { return root; }

        public boolean isLeaf(Object node) {  return ((File)node).isFile(); }

        public int getChildCount(Object parent) {
            String[] children = ((File)parent).list();
            if (children == null) return 0;
            return children.length;
        }

        public Object getChild(Object parent, int index) {
            String[] children = ((File)parent).list();
            if ((children == null) || (index >= children.length)) return null;
            return new File((File) parent, children[index]);
        }

        public int getIndexOfChild(Object parent, Object child) {
            String[] children = ((File)parent).list();
            if (children == null) return -1;
            String childname = ((File)child).getName();
            for(int i = 0; i < children.length; i++) {
                if (childname.equals(children[i])) return i;
            }
            return -1;
        }

        public void valueForPathChanged(TreePath path, Object newvalue) {}
        public void addTreeModelListener(TreeModelListener l) {}
        public void removeTreeModelListener(TreeModelListener l) {}
    }
}

1 个答案:

答案 0 :(得分:0)

感谢您的建议,找到了解决方案:

DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
root.add(addNodes(null, file));
model.reload(root);