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?
No I don't think its because your method is returning void. Try setting your TreeView root first before expanding:
If that doesn't work then try wrapping your initial expandTreeView() to run later:
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:
Ok, I had the same problem and found that I had to delay the expansion a bit like this:
You may have to try and vary the delay to get it to work in your case.