JavaFX onInputMethodTextChanged not called after focus is lost


Question

I got something like this in mi fxml file:

<TextField fx:id="id" onInputMethodTextChanged="#foo" prefWidth="200.0" promptText="" />

But when I run it, I TAB or mouse out of the TextField control and nothing happens ("foo" isn't called) .

1
4
10/18/2012 1:38:56 PM

Accepted Answer

The onInputMethodTextChanged property of TextField is applicable only if the ConditionalFeature.INPUT_METHOD is supported by the platform. To check this try

Platform.isSupported(ConditionalFeature.INPUT_METHOD)

If you are trying to do some work when the user focuses out from textfield, try

textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        if(!newValue) {
            System.out.println("Focusing out from textfield");
        }
    }
});
5
10/18/2012 2:04:59 PM

I had the same problem, even with Platform.isSupported(ConditionalFeature.INPUT_METHOD) == true

The event handler which I set with setOnInputMethodTextChanged was not firing.

I was concerned about using TextField.textProperty.addListener(ChangeListener listener) because ChangeListener.change() does not, by itself, give you easy access to the source node which triggered the event.

I worked around by using:

final TextField field = new TextField("field");
field.textProperty.addListener(new ChangeListener() {

 public void changed(ObservableValue observable, Object oldValue, Object newValue) {

   dosomethingwith(field);

}

});

Declaring field as "final" allowed me to get access to the source Node of the change event within ChangeListener.changed()


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon