2014-11-05 87 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