In my app I need to be able to remove every item from TreeView.
I have my TreeView injected i controller
private @FXML TreeView<Component> treeView;
My deletion code:
private void deleteSelectedNode() {
TreeItem<Component> node = treeView.getSelectionModel().getSelectedItem();
if (node == null) {
return;
}
TreeItem<Component> parent = node.getParent();
if (parent != null) {
parent.getChildren().remove(node);
} else {
//how to delete root item without parent?
}
}
Actual question is: how to remove root element? I could not find any method in api and I don't want to create new instance, I prefer dependency injection solution. I think I can hide this item untill next node is created, but it seems to be little hacky.
Thank you for your time spent in this topic!
treeView.setRoot(null);
should work.
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