user555
user555

Reputation: 1554

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.

Upvotes: 2

Views: 3461

Answers (1)

Elltz
Elltz

Reputation: 10859

Try this Sir,

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

Upvotes: 3

Related Questions