簡體   English   中英

顯示時,JTree復制文件夾

[英]JTree duplicates folder when showing

我有一個自定義的Jtree,它顯示給定文件夾的結構。 我的問題是,它以某種方式重復了文件夾。

例如,給定的文件夾為C:\\ Example

在示例文件夾中,有3個名為A,B和C的文件夾

JTree應該這樣查看:

C:\\示例

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

但它復制文件夾,因此顯示:

C:\\示例

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

我使用自定義渲染器僅顯示Name而不顯示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());

您在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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM