如何使用fxml在javafx中触发针对焦点的事件? [英] How to trigger an event on focus out for a textfield in javafx using fxml?

查看:414
本文介绍了如何使用fxml在javafx中触发针对焦点的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在相关fxml的控制器类中具有此功能.我需要将此功能从文本字段聚焦出来,但是场景生成器没有类似于onfocusout的事件.如何使用控件类实现此目的?

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

推荐答案

您可以将focusListener附加到TextField,然后在其中执行代码.侦听器可以附加在控制器的initialize()方法内部.

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

这篇关于如何使用fxml在javafx中触发针对焦点的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆