Search code examples
javaswinguser-interfacejtree

Desktop view in JTree - Swing - Windows Only


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

enter image description here

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.


Solution

  • 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: