Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expand TreeView (parent)nodes which contains selected item

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?

like image 537
Perneel Avatar asked Dec 14 '13 18:12

Perneel


1 Answers

Ok, I notice that in your expandTreeView() method you expand the node and then you recurse to the previous node to expand that. In my own code I expanded from the root to the leaf, so lets try that:

private static <T> void expandTreeView(TreeItem<T> selectedItem) {
    if (selectedItem != null) {
        expandTreeView(selectedItem.getParent());
        if (!selectedItem.isLeaf()) {
            selectedItem.setExpanded(true);
        }
    }
}
like image 63
Jurgen Avatar answered Dec 08 '22 06:12

Jurgen