expand TreeView (parent)nodes which contains selected item

Viewed 7248

I have a TreeView existing out of User objects. The TreeView represents the hierarchy of the Users:

  • Master1
    • Super1
    • Super2
      • User1
      • User2
    • Super3
      • User3
    • Super4
  • Master2
  • ...

Every TreeItem is Collapsed when the TreeView is initialized. However, it can be that when the FXML is loaded, a TreeItem object is passed through from another FXML file. Eg: User3 has been passed through:

selectedUserTreeItem = (TreeItem<User>) currentNavigation.getPassthroughObject(); //this is the User3 TreeItem

I try to use a recursive function to expand all the parent nodes from the selecterUserTreeItem

if (selectedUserTreeItem != null) {
    expandTreeView(selectedUserTreeItem);
}

tvUsers.setRoot(rootItem);

This is what I have so far:

private void expandTreeView(TreeItem<User> selectedItem) {        
    if (selectedItem != null) {
        System.out.println(selectedItem);
        if (selectedItem.isLeaf() == false) {
            selectedItem.setExpanded(true);
        }
        TreeItem<User> parent = selectedItem.getParent();
        expandTreeView(parent);
    } else {
        System.out.println("null");
    }
}

I think it has to do something with the fact that the function is a void function and it should be returning a TreeItem object I suppose but for some reason I don't succeed in doing it.

Could someone point me in the right direction?

3 Answers
Related