v1shva
v1shva

Reputation: 1599

How to trigger an event on focus out for a textfield in javafx using fxml?

I have this function in the controller class of the relevant fxml. I need this function to be fired on focus out from a textfield, but scene builder doesn't have an event similar to onfocusout. How to achieve this using the control class?

@FXML
private void ValidateBikeNo(){
    Tooltip error = new Tooltip("This bike no exists");
    BikeNoIn.setTooltip(error);
}

Upvotes: 4

Views: 7680

Answers (2)

ItachiUchiha
ItachiUchiha

Reputation: 36722

You can attach a focusListener to the TextField and then execute the code inside it. The listener can be attached inside the initialize() method of the controller.

public class MyController implements Initializable {
    ...
    @FXML
    private Textfield textField;

    public void initialize() {
        ...
        textField.focusedProperty.addListener((ov, oldV, newV) -> {
           if (!newV) { // focus lost
              // Your code
           }
        });
         .....
    }
}

Upvotes: 11

Dmitry
Dmitry

Reputation: 186

You have to use method textField.focusedProperty() instead of textField.focusProperty

public class MyController implements Initializable { 
...         
@FXML        
private Textfield textField;
public void initialize() {
   textField.focusedProperty().addListener((ov, oldV, newV) -> {
      if (!newV) { // focus lost
              // Your code
           }
        });
    }
}

Upvotes: 7

Related Questions