问题
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);
}
回答1:
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
}
});
.....
}
}
回答2:
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
}
});
}
}
来源:https://stackoverflow.com/questions/42943652/how-to-trigger-an-event-on-focus-out-for-a-textfield-in-javafx-using-fxml