java 在 TreeView 中设置选定的 TreeItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26929276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set selected TreeItem in TreeView
提问by john
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.
我有一个位于 GridPane 内的 TreeView。某个功能需要用户选择一个 TreeItem 并单击屏幕上的按钮。按钮关联的功能完成后,我希望焦点回到之前在TreeView中选择的TreeItem。
At the end of the button action, I have:
在按钮操作结束时,我有:
TreeItem<String> selectedItem = [TreeItem that was last selected]
TreeItem<String> selectedItem = [TreeItem that was last selected]
How can I give focus back to the TreeView with selectedItem
highlighted?
如何将焦点返回给selectedItem
突出显示的 TreeView ?
Neither the TreeView or TreeItem have a setSelected
method I can use.
TreeView 或 TreeItem 都没有setSelected
我可以使用的方法。
回答by Dave Jarvis
To select an item:
要选择一个项目:
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 );
That is, get the rowof the treeItem
from its treeView
, then pass that rowinto the treeView
's selection model.
也就是说,得到了行的treeItem
,从它的treeView
,然后传递一行到treeView
的选择模型。
Aside, the TreeView
API could be improved to delegate for a single tree item:
此外,TreeView
可以改进 API 以委托单个树项:
treeView.select( treeItem );
Unfortunately, no such method exists.
不幸的是,不存在这样的方法。
回答by eckig
TreeView.getSelectionModel()offers:
TreeView.getSelectionModel()提供:
These are protected methods, so consider using select.
这些是受保护的方法,因此请考虑使用select。
回答by mike rodent
Just to expand (...) on the comment made by malamut to the chosen answer, and also to clarify something:
只是为了扩展 (...) 对 Malamut 对所选答案的评论,并澄清一些事情:
Actually, you do not have to do two operations (find row, then select row). This works fine:
实际上,您不必执行两个操作(查找行,然后选择行)。这工作正常:
tableView.getSelectionModel().select( treeItem );
But, with this, or any other method to set selection programmatically, this will simply fail if the tree item is not showing. "Showing" is not the same as visible: a node can be showing but not be visible, for example with a ScrollPane
, where the part of the tree in question has been scrolled out of view.
但是,使用此方法或任何其他以编程方式设置选择的方法,如果树项未显示,这将简单地失败。“显示”与可见不同:节点可以显示但不可见,例如使用 a ScrollPane
,其中有问题的树部分已滚动到视图之外。
"Showing" means that all ancestors of the TreeItem
in question, up to and including the root TreeItem
, are expanded. There appears to be no built-in method to accomplish this currently (JavaFX 11). So you'd typically do something like this before attempting programmatic selection:
“显示”意味着有问题的所有祖先TreeItem
,直到并包括根TreeItem
,都被扩展。目前似乎没有内置方法可以完成此操作(JavaFX 11)。因此,在尝试程序化选择之前,您通常会执行以下操作:
for( TreeItem ti = treeItemToBeSelected; ti.getParent() != null; ti = ti.getParent() ){
ti.getParent().setExpanded( true );
}