2014-12-05 59 views
2

我想獲得一個桌面視圖中JTree,是這樣的:桌面視圖僅適用於Windows

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" 

請給我看一些指示,謝謝。

+0

另請參閱[文件瀏覽器GUI](http://codereview.stackexchange.com/q/4446/7784)。 – 2014-12-06 00:49:31

回答

3

如果我正確理解您的要求,那麼您需要使用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); 
     } 
    } 
} 

您可以使用以下方法來設置每個正確的圖標節點:

+1

非常好看,它看起來像這種方法和「C:\用戶[用戶名] \桌面」和「C:\用戶\公共\公共桌面」文件夾實際上是互補的:在我的筆記本電腦這三條路線幾乎給你整個桌面文件夾的內容。唯一缺少的兩個是「控制面板」和「回收站」。 – 2014-12-05 16:03:16

+1

謝謝你們,我在[列表](http://i.imgur.com/NK0COAh.png)中得到了我想要的一切,非常感謝你們的幫助。 – 2014-12-05 17:10:38

1

我認爲Windows桌面「特殊文件夾」(見this Wikipedia article)是幾個文件夾的組合,如「C:\ Users [用戶名] \桌面」和「C:\用戶\公共桌面」。要在樹節點中組合多個文件夾,您需要一個像your earlier question中討論的自定義樹模型。

+0

我很想試着考慮公共桌面文件夾的答案。然而,它是非常特定於操作系統的,任何Java應用程序都應該是平臺無關的,這就是爲什麼我不願意這樣做。另一方面,我已經在早期的問題中提出了你的多根建議:-) – dic19 2014-12-05 16:34:02

+1

是的,這個問題應該被標記爲僅限於Windows。對於OS X和Linux,您可能會採取完全不同的方法。 – 2014-12-05 16:38:15

相關問題