如何使树视图与图像一起使用

时间:2016-05-05 16:03:38

标签: javafx treeview

我有一个树视图,根据文件类型显示图像。这是构造函数的代码:

public FileTreeItem(File f)
{
    super(f);
            String str = getFileExtension(f);
            String imagePath = !f.isFile() ? "file:ca/forgetech/ide/assets/images/packageFull.png":
                    str.equals("is") ? "file:ca/forgetech/ide/assets/images/instructionset.png":
                    str.equals("dt") ? "file:ca/forgetech/ide/assets/images/datatype.png":
                    str.equals("main") ? "file:ca/forgetech/ide/assets/images/startfile.png":
                    str.equals("asm") ? "file:ca/forgetech/ide/assets/images/assemblyfile.png":
                    str.equals("png") || str.equals("jpg") || str.equals("bmp") ? "file:ca/forgetech/ide/assets/images/imagefile.png":
                    "file:ca/forgetech/ide/assets/images/blankfile.png";
            ImageView imgView = new ImageView(new Image(imagePath));
            setGraphic(imgView);
    }

    private String getFileExtension(File f)
    {
        int i = f.getAbsolutePath().lastIndexOf(".");
        String ret = "";
        if(i != -1)
        {
            ret = f.getAbsolutePath().substring(i);
        }
        return ret;
    }

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。图像位置之前的“file:”部分导致其工作原理出现问题。你不要把它放在图像位置之前。