没有叶子的JTree

时间:2012-11-06 07:22:18

标签: java swing jtree treenode

是否可以制作没有叶子的JTree?如果有可能请告诉我。

JTree image

我想将这些突出显示的叶子转换为文件夹或父级。

如果您想要除此之外的其他任何东西,请告诉我。

2 个答案:

答案 0 :(得分:2)

如此FileTreeModel所示,isLeaf()应返回falsegetChildCount()应返回0目录。结果如图here所示;虽然不明显,但test目录为空。

@Override
public boolean isLeaf(Object node) {
    File f = (File) node;
    return !f.isDirectory();
}

@Override
public int getChildCount(Object parent) {
    File f = (File) parent;
    if (!f.isDirectory()) {
        return 0;
    } else {
        return f.list().length;
    }
}

image

答案 1 :(得分:1)

我想如果你总是从你的TreeModel中的isLeaf返回true,但是从你的叶子节点的getChildCount返回0你将得到你想要的。

相关问题