Java fx Delete tree item treeview

In my application, I need to remove every item from the TreeView.

I have a TreeView controller with i /

private @FXML TreeView<Component> treeView;

      

My removal 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?           
    }
}

      

The burning question is: how to remove the root element? I couldn't find any method in the api and I don't want to create a new instance, I prefer the dependency injection solution. I think I can hide this element until the next node is created, but it seems to be a bit of a hack.

Thanks for your time on this thread!

+3


source to share


1 answer


treeView.setRoot(null);

      



must work.

+5


source







All Articles