[英]How to select a treeItem in a treeView, after checking a Condition on that treeItem, in Javafx
I have downloaded familyTree project and it doesn't have the ability to search for an specific family member! 我已经下载了familyTree项目,但是它无法搜索特定的家庭成员!
I have added a search button to it, and in the handler method section, I need the code that searches for a member that has the specified socialID , and select it (scroll it to the sight, and make it blue (selected)). 我已经添加了一个搜索按钮,并且在处理程序方法部分中,我需要代码来搜索具有指定的socialID的成员,然后选择它(将其滚动到视线,然后使其变为蓝色(选中))。 But I don't know how to programmatically select a treeItem, and make it visible and selected?
但是我不知道如何以编程方式选择一个treeItem,并使它可见并被选中?
My code: 我的代码:
@FXML
private void btnSearch_click(ActionEvent event){
for(TreeItem<FamilyMember> treeItem:root.getChildren()){
if(treeItem.getValue().getNationality().toString()=="22"){
// treeView.setSelectionModel(item);
treeView.getSelectionModel().select(treeItem);
//it still doesnt select the item with nationality=="22"
break;
}
}
}
You can select the item with 您可以选择
treeView.getSelectionModel().select(item);
and if you still need to scroll (I think selecting it might automatically scroll to it), do 如果仍然需要滚动(我认为选择它可能会自动滚动到它),请执行
treeView.scrollTo(treeView.getRow(item));
A couple of notes: 一些注意事项:
I do not understand the for
loop. 我不明白
for
循环。 Why are you doing 你为什么这么做
TreeItem<FamilyMember> item = root.getChildren().get(i);
and why are you creating the index i
? 为什么要创建索引
i
? What is wrong with the treeItem
variable you already defined in the loop syntax? 您已经在循环语法中定义的
treeItem
变量怎么了? Isn't this necessarily exactly the same thing as item
? 这不一定与
item
完全相同吗?
You need to read How do I compare strings in Java? 您需要阅读如何比较Java中的字符串?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.