JavaFX – set two root nodes for TreeView

Is there a way to set two root nodes for a treeview?

I found many examples if simple treeview, but no useful examples of my situation

Solution

No: the tree has only one root node

To achieve the desired effect, create a virtual root node and add its two nodes to it Create a treeview using the virtual root node and call tree Setshowroot (false), so the virtual node will 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 ;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>