omidXxX
omidXxX

Reputation: 147

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!

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)). But I don't know how to programmatically select a treeItem, and make it visible and selected?

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;
              }

    }
}

Upvotes: 0

Views: 1624

Answers (1)

James_D
James_D

Reputation: 209319

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:

  1. I do not understand the for loop. Why are you doing

    TreeItem<FamilyMember> item = root.getChildren().get(i);
    

    and why are you creating the index i? What is wrong with the treeItem variable you already defined in the loop syntax? Isn't this necessarily exactly the same thing as item?

  2. You need to read How do I compare strings in Java?

Upvotes: 3

Related Questions