简体   繁体   中英

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(); . My goal is to be able to reselect that treeView item once the treeView is rebuild. 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.

I've tried treeView.getSelectionModel().select(index); but that seem to only work when the treeItem's parent is expanded. The API doesn't mention anything about that the item to be selected must be under a node that is expanded. I also tried 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.

Try this Sir,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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