Set two root nodes for TreeView


Question

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
15
3/7/2014 8:26:26 PM

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 ;
}
44
3/8/2014 1:42:21 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon