Set two root nodes for TreeView

2019-02-12 06:05发布

Is there a way to set two root nodes for one TreeView?

I found many example if simple TreeView but there is no useful example for my case.

1条回答
孤傲高冷的网名
2楼-- · 2019-02-12 06:15

No: a tree only has one root node.

To get the effect you want, create a dummy root node and add your two nodes to it. Create the TreeView with the dummy root node and call tree.setShowRoot(false), so the dummy node does not appear.

final TreeItem<String> root1 = new TreeItem<>("root 1");
final TreeItem<String> root2 = new TreeItem<>("root 2");
final TreeView<String> tree = createTreeView(root1, root2);

// ...

private TreeView<String> createTreeView(TreeItem<String> root1, TreeItem<String> root2) {
    TreeItem<String> dummyRoot = new TreeItem<>();
    dummyRoot.getChildren().addAll(root1, root2);
    TreeView<String> tree = new TreeView<>(dummyRoot);
    tree.setShowRoot(false);
    return tree ;
}
查看更多
登录 后发表回答