在TreeView中设置选定的TreeItem [英] Set selected TreeItem in TreeView

查看:249
本文介绍了在TreeView中设置选定的TreeItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于GridPane内的TreeView。某个功能要求用户选择TreeItem并单击屏幕上的按钮。完成与按钮关联的功能后,我希望焦点返回到先前在TreeView中选择的TreeItem。

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]

如何使用 selectedItem将焦点重新放回TreeView 突出显示?

TreeView或TreeItem都没有我可以使用的 setSelected 方法。

Neither the TreeView or TreeItem have a setSelected method I can use.

推荐答案

选择项目:

TreeView treeView = ... ; // initialize this
TreeItem treeItem = ... ; // initialize this, too
MultipleSelectionModel msm = treeView.getSelectionModel();

// This line is the not-so-clearly documented magic.
int row = treeView.getRow( treeItem );

// Now the row can be selected.
msm.select( row );

即获取 treeItem 从其 treeView ,然后将传递到 treeView 的选择模型。

That is, get the row of the treeItem from its treeView, then pass that row into the treeView's selection model.

除此之外,可以改进 TreeView API以委托单个树项:

Aside, the TreeView API could be improved to delegate for a single tree item:

treeView.select( treeItem );

不幸的是,不存在这样的方法。

Unfortunately, no such method exists.

这篇关于在TreeView中设置选定的TreeItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆