简体   繁体   English

如何获取和选择JavaFX中索引不可见的treeItem?

[英]How do I get and select a treeItem that is not visible by index in JavaFX?

I'm saving the index of the current select treeItem by using treeView.getSelectionModel().getSelectedIndex(); 我通过使用treeView.getSelectionModel().getSelectedIndex();保存当前选择的TreeItem的索引treeView.getSelectionModel().getSelectedIndex(); . My goal is to be able to reselect that treeView item once the treeView is rebuild. 我的目标是,一旦重新构建treeView,便能够重新选择该treeView项。 However there doesn't seem to be any method in the API that will allow me to get and ultimately select a treeView item that isn't under an expanded node. 但是,API中似乎没有任何方法可以让我获取并最终选择不在扩展节点下的treeView项。

I've tried treeView.getSelectionModel().select(index); 我已经尝试过treeView.getSelectionModel().select(index); but that seem to only work when the treeItem's parent is expanded. 但这似乎仅在treeItem的父项展开时有效。 The API doesn't mention anything about that the item to be selected must be under a node that is expanded. API没有提及要选择的项目必须在展开的节点下的任何内容。 I also tried treeView.getTreeItem(index).getParent().setExpanded(true); 我也尝试了treeView.getTreeItem(index).getParent().setExpanded(true); to expand the item's parent node but that also only seem to work if the item is visible and under an expanded node. 扩展项目的父节点,但似乎仅在该项目可见且处于扩展节点下时才起作用。

So my question is, how do I store the current selected treeItem and re-select it when the treeView is rebuilded. 所以我的问题是,如何存储当前选定的treeItem并在重建treeView时重新选择它。

Try this Sir, 试试这个先生,

int i;
i = treeView.getSelectionModel().getSelectedIndex();
treeView.getTreeItem(i).setExpanded(true);
treeView.getSelectionModel().select(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM