I have a TreeView that is inside a GridPane. A certain function requires the user to select a TreeItem and click on button on the screen. After the function associated with the button is completed, I want the focus to go back to the TreeItem that was previously selected in the TreeView.
At the end of the button action, I have:
TreeItem<String> selectedItem = [TreeItem that was last selected]
How can I give focus back to the TreeView with selectedItem
highlighted?
Neither the TreeView or TreeItem have a setSelected
method I can use.
I think TreeView.getSelectionModel() is your friend.
It offers:
To select an item:
That is, get the row of the
treeItem
from itstreeView
, then pass that row into thetreeView
's selection model.Aside, the
TreeView
API could be improved to delegate for a single tree item:Unfortunately, no such method exists.