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.
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 ;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With