当前位置:  开发笔记 > 编程语言 > 正文

显示文件名称并在JTree中的文件夹下选择它

如何解决《显示文件名称并在JTree中的文件夹下选择它》经验,为你挑选了1个好方法。

我设法到目前为止使用stackoverflow示例,JTree显示所有系统驱动器和文件夹,想要显示文件夹中的所有相应文件,得到循环中的所有文件名需要添加它们,这就是我卡住的地方!

请指教我添加文件夹下的文件,谢谢!

码:

public class viewGui extends JFrame {

    private FileSystemView fileSystemView;
    private Desktop desktop;
    private static final long serialVersionUID = 1083130296343096642L;
    public static JTree tree;
    private DefaultTreeModel treeModel;
    private JTable table;
    private ListSelectionListener listSelectionListener;

    private static final LayoutManager H = new GridLayout(1, 0);
    private static final LayoutManager V = new GridLayout(0, 1);

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    viewGui mainWindow = new viewGui();
                    mainWindow.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public viewGui() {

        fileSystemView = FileSystemView.getFileSystemView();
        desktop = Desktop.getDesktop();

        this.setTitle("Student Record Book");
        getContentPane().setLayout(H);
        getContentPane().setLayout(V);
        this.setPreferredSize(new Dimension(1200, 800));
        this.setExtendedState(NORMAL);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(null);
        this.setResizable(true);

        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        treeModel = new DefaultTreeModel(root);

        TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent tse){
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)tse.getPath().getLastPathComponent();
                System.out.println("Node: "+node);
                showChildren(node);
            }
        };

        File[] roots = fileSystemView.getRoots();
        for (File fileSystemRoot : roots) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot);
            root.add( node );
            File[] files = fileSystemView.getFiles(fileSystemRoot, true);
            for (File file : files) {
                if (file.isDirectory()) {
                    node.add(new DefaultMutableTreeNode(file));
                }
            }
        }

        tree = new JTree(treeModel);
        tree.setBounds(10, 11, 387, 740);
        tree.setRootVisible(false);
        tree.addTreeSelectionListener(treeSelectionListener);
        tree.expandRow(0);
        JScrollPane treeScroll = new JScrollPane(tree);

        tree.setVisibleRowCount(15);

        Dimension preferredSize = treeScroll.getPreferredSize();
        Dimension widePreferred = new Dimension(200,(int)preferredSize.getHeight());
        treeScroll.setPreferredSize( widePreferred );

        this.setLayout(H);
        this.validate();
        this.add(treeScroll, BorderLayout.WEST);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        table = new JTable();
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setAutoCreateRowSorter(true);
        table.setShowVerticalLines(false);

        listSelectionListener = new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent lse) {
                int row = table.getSelectionModel().getLeadSelectionIndex();
            }
        };

        table.getSelectionModel().addListSelectionListener(listSelectionListener);
        JScrollPane tableScroll = new JScrollPane(table);
        Dimension d = tableScroll.getPreferredSize();
        tableScroll.setPreferredSize(new Dimension((int)d.getWidth(), (int)d.getHeight()/2));
        getContentPane().add(tableScroll, BorderLayout.CENTER);


    }

    private void showChildren(final DefaultMutableTreeNode node) {
        tree.setEnabled(false);

        SwingWorker worker = new SwingWorker() {
            @Override
            public Void doInBackground() {
                File file = (File) node.getUserObject();
                if (file.isDirectory()) {
                    File[] files = fileSystemView.getFiles(file, true); //!!
                    if (node.isLeaf()) {
                        for (File child : files) {

                            System.out.println("child:"+child);
                            if (child.isDirectory()) {
                                publish(child);
                                //Need to add the file names under the folder
                            }
                        }
                    }
                }
                return null;
            }

            @Override
            protected void process(List chunks) {
                for (File child : chunks) {
                    node.add(new DefaultMutableTreeNode(child));
                    if (child.isDirectory()){

                    }
                }
            }

            @Override
            protected void done() {
                tree.setEnabled(true);
            }
        };
        worker.execute();
    }
}

trashgod.. 7

不要在循环中添加所有文件名.相反,创建一个FileTreeModel实现TreeModel,如图所示这里.实现简单的调用File方法listFiles()getChild()getIndexOfChild().然后,您可以创建一个树并展开任何所需的行; 使用setSelectionPath()如图所示这里.

TreeModel model = new FileTreeModel(new File(System.getProperty("user.dir")));
JTree tree = new JTree(model);
tree.expandRow(0);

图片

我只得到了c:\; 请给我指示获取所有系统驱动器等.

您可以获取文件系统根目录列表File.listRoots(),如在Java查找所有驱动器号,或者FileSystemView#getRoots()文件浏览器GUI中所示.



1> trashgod..:

不要在循环中添加所有文件名.相反,创建一个FileTreeModel实现TreeModel,如图所示这里.实现简单的调用File方法listFiles()getChild()getIndexOfChild().然后,您可以创建一个树并展开任何所需的行; 使用setSelectionPath()如图所示这里.

TreeModel model = new FileTreeModel(new File(System.getProperty("user.dir")));
JTree tree = new JTree(model);
tree.expandRow(0);

图片

我只得到了c:\; 请给我指示获取所有系统驱动器等.

您可以获取文件系统根目录列表File.listRoots(),如在Java查找所有驱动器号,或者FileSystemView#getRoots()文件浏览器GUI中所示.

推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有