Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FX Delete TreeView root item

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!

like image 564
Bartosz Kraszewski Avatar asked Jul 02 '26 21:07

Bartosz Kraszewski


1 Answers

treeView.setRoot(null);

should work.

like image 50
James_D Avatar answered Jul 05 '26 11:07

James_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!