简体   繁体   English

在 TreeView 中设置选定的 TreeItem

[英]Set selected TreeItem in TreeView

I have a TreeView that is inside a GridPane.我有一个位于 GridPane 内的 TreeView。 A certain function requires the user to select a TreeItem and click on button on the screen.某个功能需要用户选择一个 TreeItem 并单击屏幕上的按钮。 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.按钮关联的功能完成后,我希望焦点回到之前在TreeView中选择的TreeItem。

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?如何将焦点返回给突出显示selectedItem的 TreeView?

Neither the TreeView or TreeItem have a setSelected method I can use. TreeView 或 TreeItem 都没有我可以使用的setSelected方法。

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 row of the treeItem from its treeView , then pass that row into the treeView 's selection model.也就是说,从其treeView获取treeItem,然后将该传递到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.不幸的是,不存在这样的方法。

TreeView.getSelectionModel() offers: TreeView.getSelectionModel()提供:

These are protected methods, so consider using select .这些是受保护的方法,因此请考虑使用select

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. “显示”与可见不同:节点可以显示但不可见,例如使用ScrollPane ,其中有问题的树部分已滚动到视图之外。

"Showing" means that all ancestors of the TreeItem in question, up to and including the root TreeItem , are expanded. “显示”意味着所讨论的TreeItem所有祖先,直到并包括根TreeItem ,都被展开。 There appears to be no built-in method to accomplish this currently (JavaFX 11).目前似乎没有内置方法可以完成此操作(JavaFX 11)。 So you'd typically do something like this before attempting programmatic selection:因此,在尝试程序化选择之前,您通常会执行以下操作:

for( TreeItem ti = treeItemToBeSelected; ti.getParent() != null; ti = ti.getParent() ){
    ti.getParent().setExpanded( true );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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