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

风流意气都作罢 提交于 2020-01-11 11:56:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!