0

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

    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
omidXxX
  • 147
  • 1
  • 1
  • 9
  • If the answer worked, you should mark it as correct – James_D May 31 '17 at 20:32
  • Please don't change your question into an answer. If there is more detail you wish to add for the benefit of future readers, please add an answer post. – halfer Jun 01 '17 at 14:45

1 Answers1

3

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?

James_D
  • 201,275
  • 16
  • 291
  • 322