Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set two root nodes for TreeView

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.

like image 242
Peter Penzov Avatar asked Mar 07 '14 20:03

Peter Penzov


1 Answers

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 ;
}
like image 85
James_D Avatar answered Sep 21 '22 18:09

James_D