JTree在显示时复制文件夹

时间:2014-11-05 12:40:30

标签: java swing jtree

我有一个自定义Jtree,它显示给定文件夹的结构。我的问题是它以某种方式重复了文件夹。

例如,给定的文件夹是C:\ Example

在示例文件夹中有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());

1 个答案:

答案 0 :(得分: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;
    }

此外,您无需使用null参数调用它,如下所示调用:

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