Java FX Delete TreeView root item


Question

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!

1
3
9/4/2014 6:02:34 PM

Accepted Answer

treeView.setRoot(null);

should work.

6
9/4/2014 6:13:34 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon