展开包含所选项目的TreeView(父)节点 [英] expand TreeView (parent)nodes which contains selected item
问题描述
我有一个存在于User对象之外的TreeView. TreeView代表用户的层次结构:
I have a TreeView existing out of User objects. The TreeView represents the hierarchy of the Users:
- Master1
- Super1
- 超级2
- User1
- User2
- Master1
- Super1
- Super2
- User1
- User2
- User3
初始化TreeView时,每个TreeItem都会折叠.但是,可能是在加载FXML时,从另一个FXML文件传递了TreeItem对象.例如:User3已通过:
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
我尝试使用递归函数来扩展
selecterUserTreeItem
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"); } }
我认为它必须做一些事情,即该函数是一个void函数,它应该返回一个我猜想的TreeItem对象,但是由于某种原因,我没有成功完成它.
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?
推荐答案
好吧,我注意到在您的expandTreeView()方法中,您将节点展开,然后递归到前一个节点以对其进行展开.在我自己的代码中,我从根扩展到了叶,所以让我们尝试一下:
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 void expandTreeView( TreeItem<User> selectedItem ) { if ( selectedItem != null ) { expandTreeView( selectedItem.getParent() ); if ( ! selectedItem.isLeaf() ) { selectedItem.setExpanded( true ); } } }
这篇关于展开包含所选项目的TreeView(父)节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!