如何在TextField中的文本更改时获取事件? JavaFX的

时间:2015-07-12 17:20:29

标签: events javafx textfield

如何更改TextField的文本时如何生成新事件?

3 个答案:

答案 0 :(得分:12)

使用TextField textProperty

注册听众
textField.textProperty().addListener((obs, oldText, newText) -> {
    System.out.println("Text changed from "+oldText+" to "+newText);
    // ...
});

答案 1 :(得分:8)

或使用ChangeListener界面。

textField.textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable,
            String oldValue, String newValue) {

        System.out.println(" Text Changed to  " + newValue + ")\n");
    }
});

答案 2 :(得分:-1)

我的解决方案:

....
String temp;
....
//previously in some method when starting to take initial value of the textfield and save it in a temporal
temp=id_textfield.getText();
....
//when losing focus we check if the new value is different

id_textfield.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

            //focus in
            if (newValue) {
                temp = id_textfield.getText();
            }

            //focus out
            if (oldValue) {

                if (!(temp.equals(id_textfield.getText()))) {
                    System.out.println("New String")
                }
            }

        }
    });