【问题标题】:List files and directories with Jtree and File in Java在 Java 中使用 Jtree 和 File 列出文件和目录
【发布时间】:2014-07-11 08:44:10
【问题描述】:

我想用 JTree 创建一个非常非常简单的文件管理器,但是我只看到了一个非常硬的代码,我想创建这个脚本非常干净和简单。

你能帮帮我吗?如何在 JTree 中列出我的计算机目录?

【问题讨论】:

  • 所以...您已经找到了执行此操作的现有代码(在其他问题中)但发现它太难理解并且您希望这可以以更简单的方式完成?你能给我们指出其中的一些代码吗?
  • 好的,但请帮帮我
  • 你有没有看我的评论?我让你向我们展示你一直在努力理解的代码。
  • 而且....你已经赢得了你的反对票。我很难看到你努力的任何证据。这里没有人会用勺子喂你一些可爱的、易于理解的代码来解决你的问题。
  • 好吧,我只需要帮助..如果你能帮助好吧...

标签: java jtree file-management


【解决方案1】:

我写了我能想到的最简单的文件浏览器示例。它列出了 Windows 计算机上 C: 驱动器上的所有目录和文件。

这是结果。

这是代码。我将所有内容放在一个类中,以便更容易粘贴到这里。你应该把类分开。

package com.ggl.testing;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class FileBrowser implements Runnable {

    private DefaultMutableTreeNode root;

    private DefaultTreeModel treeModel;

    private JTree tree;

    @Override
    public void run() {
        JFrame frame = new JFrame("File Browser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        File fileRoot = new File("C:/");
        root = new DefaultMutableTreeNode(new FileNode(fileRoot));
        treeModel = new DefaultTreeModel(root);

        tree = new JTree(treeModel);
        tree.setShowsRootHandles(true);
        JScrollPane scrollPane = new JScrollPane(tree);

        frame.add(scrollPane);
        frame.setLocationByPlatform(true);
        frame.setSize(640, 480);
        frame.setVisible(true);

        CreateChildNodes ccn = 
                new CreateChildNodes(fileRoot, root);
        new Thread(ccn).start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new FileBrowser());
    }

    public class CreateChildNodes implements Runnable {

        private DefaultMutableTreeNode root;

        private File fileRoot;

        public CreateChildNodes(File fileRoot, 
                DefaultMutableTreeNode root) {
            this.fileRoot = fileRoot;
            this.root = root;
        }

        @Override
        public void run() {
            createChildren(fileRoot, root);
        }

        private void createChildren(File fileRoot, 
                DefaultMutableTreeNode node) {
            File[] files = fileRoot.listFiles();
            if (files == null) return;

            for (File file : files) {
                DefaultMutableTreeNode childNode = 
                        new DefaultMutableTreeNode(new FileNode(file));
                node.add(childNode);
                if (file.isDirectory()) {
                    createChildren(file, childNode);
                }
            }
        }

    }

    public class FileNode {

        private File file;

        public FileNode(File file) {
            this.file = file;
        }

        @Override
        public String toString() {
            String name = file.getName();
            if (name.equals("")) {
                return file.getAbsolutePath();
            } else {
                return name;
            }
        }
    }

}

【讨论】:

  • 参见TreeModel引用here的实现。
  • @trashgod:是的。在此示例中,我对 C: 驱动器上的文件和目录进行了深度优先搜索。我正在开发一个更复杂的文件浏览器,它具有 TreeModel 实现和广度优先搜索。
  • 这是我的Java Swing File Browser 文章。
  • @Gilbert Le Blanc,如果我想显示 d 文件夹而不是 c 文件夹怎么办..帮助
  • @Nihal Chandwani:将代码更改为以 D:/ 开头。此代码并非要替代 Windows 文件资源管理器。
【解决方案2】:

这就是我在项目中遇到问题时解决问题的方法,该项目涉及一个 jtree 来显示我的扫描结果。

scanner函数用于初始化参数并调用displayDirectoryContents函数

以下是我使用的方法的一些缺点

  • 它将空文件夹显示为叶节点,而不是将它们显示为 没有孩子的叶子
  • 模型应该通过右键属性并清除模型来手动清除

    使用的功能

public void displayDirectoryContents(File dir, DefaultMutableTreeNode root2)
       throws InterruptedException {

    DefaultMutableTreeNode newdir = new DefaultMutableTreeNode();

    // creates array of file type for all the files found
    File[] files = dir.listFiles();

    for (File file : files) {
        if (file == null) {
            System.out.println("NUll directory found ");
            continue;
        }
        if (file.isDirectory()) {
            // file is a directory that is a folder has been dound

            if (file.listFiles() == null) {
                // skips null files
                continue;
            }

            // gets the current model of the jtree
            DefaultTreeModel model = (DefaultTreeModel) result.getModel();

            // gets the root
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();

            // generates a node newdir using filename
            newdir = new DefaultMutableTreeNode(file.getName());

            // adds a node to the root of the jtree
            root2.add(newdir);

            // refresh the model to show the changes
            model.reload();

            // recursively calls the function again to explore the contents
            // folder
            displayDirectoryContents(file, newdir);
        } else {
            // Else part File is not a directory

            // gets the current model of the tree
            DefaultTreeModel model = (DefaultTreeModel) result.getModel();

            // selected node is the position where the new node will be
            // inserted
            DefaultMutableTreeNode selectednode = root2;

            DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName());

            // inserts a node newfile under selected node which is the root
            model.insertNodeInto(newfile, selectednode, selectednode.getChildCount());

            // refresh the model to show the changes
            model.reload();

        }

    }
}

Scanner Function 初始化上述函数

public void scanner() throws InterruptedException {
    // creates a file with the location filename
    String location = "C:\\Users\\Ashish Padalkar\\Documents";
    File currentDir = new File(location);

    // result is the variable name for jtree
    DefaultTreeModel model = (DefaultTreeModel) result.getModel();
    // gets the root of the current model used only once at the starting
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    // function caled
    displayDirectoryContents(currentDir, root);
}

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2011-03-01
    • 1970-01-01
    • 2012-06-20
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    相关资源
    最近更新 更多