Desktop view in JTree - Swing - Windows Only

六月ゝ 毕业季﹏ 提交于 2019-12-24 04:25:16

问题


I would like to get a desktop view in JTree, something like this:

I have a sample code which shows only the c:\, since I am new to Java, finding difficulties to achieve it.

Here is my code so far:

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

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

SO far I have tried to change 'System Properties' but it didn't work:

"user.dir"

Please show me show me some directions, thanks.


回答1:


If I understand your requirements correctly, then you need to use FileSystemView class in order to get OS specific data such as root partitions. Since the JDK1.1 File API doesn't allow access OS related information.

Note: in Windows Desktop folder is considered a root node. For example, running on Windows the following snippet should print the folders that you see in your desktop, pretty much like the picture you've included:

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

You can use the following methods to set the correct icon for each node:

  • isComputerNode(File file)
  • isDrive(File file)
  • isFloppyDrive(File file)
  • isFileSystem(File file)



回答2:


I think the Windows Desktop "special folder" (see this Wikipedia article) is a combination of several folders, like "C:\Users[username]\Desktop" and "C:\Users\Public\Public Desktop". To combine more than one folder in a tree node, you would need a custom tree model like the one discussed in your earlier question.



来源:https://stackoverflow.com/questions/27318486/desktop-view-in-jtree-swing-windows-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!