首页 > 解决方案 > JTree expandPath 适用于 File 但不适用于包装类

问题描述

我想知道有什么要求才能JTree正常工作。我已经编写了一些代码,显示了一个文件系统树,然后调用expandPath()它来扩展它的路径。

当我使用 File 对象时,一切正常,但树显示每个节点上的完整路径。所以我将它包装在一个更改的类中,toString()以便只显示目录/文件名,但现在expandPath()不再工作并且路径没有扩展。

package tools.controls.TreeControl.Filesystem;

import java.io.File;
import java.net.URI;

public class TreeFile
    extends File
    implements Cloneable
    , Comparable<File>
{
    private static final long serialVersionUID = 1L;

    public TreeFile(TreeFile oOther)
    {
        super(oOther.getParentFile(), oOther.getName());
    }

    public TreeFile(File oFile)
    {
        super(oFile.getParentFile(), oFile.getName());
    }

    public TreeFile(String oPath)
    {
        super(oPath);
    }

    public TreeFile(URI oFileURL)
    {
        super(oFileURL);
    }

    public TreeFile(String oParent, String oChild)
    {
        super(oParent, oChild);
    }

    public TreeFile(File oParent, String oChild)
    {
        super(oParent, oChild);
    }

    public TreeFile getParentTreeFile()
    {
        File f = getParentFile();
        if(f == null)
            return null;

        return new TreeFile(f);
    }

    public String toString()
    {
        return getName();
    }

    public TreeFile clone()
    {
        return new TreeFile(this);
    }

    @Override
    public int compareTo(File oObject)
    {
        return super.compareTo(oObject);
    }
}

标签: javaswingtreejtree

解决方案


推荐阅读