java - JTree 中的桌面 View - Swing - 仅限 Windows

标签 java swing user-interface jtree

我想在 JTree 中获得桌面 View ,如下所示:

enter image description here

我有一个sample code它仅显示 c:\,因为我是 Java 新手,发现实现它很困难。

这是迄今为止我的代码:

public class FileTreeDemo {

    public static void main(String[] args) {
        File root;
        if (args.length > 0) {
            root = new File(args[0]);
        } else {
            root = new File(System.getProperty("user.home"));
        }

        System.out.println(root);
        FileTreeModel model = new FileTreeModel(root);

        JTree tree = new JTree();
        tree.setModel(model);
        tree.setRootVisible(true);
        tree.setShowsRootHandles(true);

        JScrollPane scrollpane = new JScrollPane(tree);

        JFrame frame = new JFrame("FileTreeDemo");
        frame.getContentPane().add(scrollpane, "Center");
        frame.setSize(400,600);
        frame.setVisible(true);    
    }
}

FileTreeModel 类

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

到目前为止,我已尝试更改“系统属性”,但没有成功:

"user.dir"

请告诉我一些方向,谢谢。

最佳答案

如果我正确理解您的要求,那么您需要使用FileSystemView类以获得操作系统特定的数据,例如根分区。由于 JDK1.1 File API 不允许访问操作系统相关信息。

注意:在 Windows 中 Desktop 文件夹被视为根节点。例如,在 Windows 上运行以下代码片段应打印您在桌面上看到的文件夹,与您包含的图片非常相似:

FileSystemView fileSystemView = FileSystemView.getFileSystemView();
for (File file : fileSystemView.getRoots()) {
    System.out.println("Root: " + file);
    for (File f : file.listFiles()) {
        if (f.isDirectory()) {
            System.out.println("Child: " + f);
        }
    }
}

您可以使用以下方法为每个节点设置正确的图标:

关于java - JTree 中的桌面 View - Swing - 仅限 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27318486/

相关文章:

java - Quaqua 外观问题

java - 使用ssl运行的tomcat的两个实例

Java - 获取扩展器(子)属性

java - 平滑 Swing 动画

java - 刷新 JPanel

user-interface - 如何在 .app 包中包装命令行可执行文件?

flutter - 如何在Flutter中在图像上叠加图标?

java - 用于计算量大的程序的 GUI 界面

java - 我怎样才能得到链接的 HashMap

java - 绑定(bind)不匹配错误