Reputation: 1599
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
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
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