2014-11-05 51 views
0

我有一個顯示給定文件夾結構的自定義Jtree。我的問題是,它以某種方式複製文件夾。JTree在顯示時複製文件夾

例如給定的文件夾是C:\實施例

在該例子中夾有3文件夾中稱爲A,B,C

的JTree的假定,以查看它是這樣的:

C: \實施例

->A 
    some1.txt 
->B 
->C 

但它重複文件夾,以便它顯示:

C:\實例

->A 
    ->A 
    some1.txt 
->B 
    ->B 
->C 
    ->C 

我使用自定義渲染器只顯示名稱未在JTree的完整路徑, 那就是:

@Override 
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { 
     System.out.println(value); 
     super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); 
     if (value instanceof DefaultMutableTreeNode) { 
      value = ((DefaultMutableTreeNode)value).getUserObject(); 
      if (value instanceof File) { 
       File file = (File) value; 
       if (file.isFile()) { 
        setText(file.getName()); 
       } else { 
        setText(file.getName()); 
       } 
      } 
     } 
     return this; 
    } 

這裏是我用數據填充JTree的:

/** 
* Add nodes from under "dir" into curTop. Highly recursive. 
*/ 
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) { 

    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(dir); 
    if (curTop != null) { // should only be null at root 
     curTop.add(curDir); 
    } 
    File[] tmp = dir.listFiles(); 
    Vector<File> ol = new Vector<File>(); 

    ol.addAll(Arrays.asList(tmp)); 
    Collections.sort(ol, new Comparator<File>() { 
     @Override 
     public int compare(File o1, File o2) { 

      int result = o1.getName().compareTo(o2.getName()); 

      if (o1.isDirectory() && o2.isFile()) { 
       result = -1; 
      } else if (o2.isDirectory() && o1.isFile()) { 
       result = 1; 
      } 

      return result; 
     } 
    }); 
    // Pass two: for files. 
    for (int fnum = 0; fnum < ol.size(); fnum++) { 
     File file = ol.elementAt(fnum); 
     DefaultMutableTreeNode node = new DefaultMutableTreeNode(file); 
     if (file.isDirectory()) { 
      addNodes(node, file); 
     } 
     curDir.add(node); 
    } 
    return curDir; 
} 

這裏是我在我的代碼中使用這些:

 dir = new File(System.getProperty("user.dir")+"\\Example"); 
    tree = new JTree(addNodes(null, dir)); 
    tree.setCellRenderer(new MyTreeCellRenderer()); 
+0

1)爲了更好地儘快提供幫助,發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)。 2)查看一個工作示例的[File Browser GUI](http://codereview.stackexchange.com/q/4446/7784)。 – 2014-11-05 23:01:57

回答

0

你必須在addNodes(..)方法的遞歸問題,如接下來的更改:

DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) { 

     File[] tmp = dir.listFiles(); 
     Vector<File> ol = new Vector<File>(); 
     ol.addAll(Arrays.asList(tmp)); 
     Collections.sort(ol, new Comparator<File>() { 
      @Override 
      public int compare(File o1, File o2) { 

       int result = o1.getName().compareTo(o2.getName()); 

       if (o1.isDirectory() && o2.isFile()) { 
        result = -1; 
       } else if (o2.isDirectory() && o1.isFile()) { 
        result = 1; 
       } 

       return result; 
      } 
     }); 
     // Pass two: for files. 
     for (int fnum = 0; fnum < ol.size(); fnum++) { 
      File file = ol.elementAt(fnum); 
      DefaultMutableTreeNode node = new DefaultMutableTreeNode(file); 
      if (file.isDirectory()) { 
       addNodes(node, file); 
      } 
      curTop.add(node); 
     } 
     return curTop; 
    } 

你也need't與null參數調用它,就像打電話未來:

JTree tree = new JTree(addNodes(new DefaultMutableTreeNode(dir), dir)); 
+0

非常感謝你解決了我的問題:) – 2014-11-06 12:16:55

+0

不客氣 – alex2410 2014-11-06 12:20:19