焦点丢失后未调用JavaFX onInputMethodTextChanged

时间:2012-10-18 13:38:56

标签: java javafx-2 javafx

我在mi fxml文件中得到了类似的内容:

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

但是当我运行它时,我将TAB或鼠标移出TextField控件并且没有任何反应(“foo”未被调用)。

2 个答案:

答案 0 :(得分:5)

onInputMethodTextChanged TextField属性仅适用于平台支持ConditionalFeature.INPUT_METHOD的情况。要检查此尝试

Platform.isSupported(ConditionalFeature.INPUT_METHOD)

如果您在用户从文本字段中缩小时尝试执行某些操作,请尝试

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

答案 1 :(得分:0)

我有同样的问题,即使使用Platform.isSupported(ConditionalFeature.INPUT_METHOD)== true

我使用setOnInputMethodTextChanged设置的事件处理程序未触发。

我担心使用TextField.textProperty.addListener(ChangeListener侦听器),因为ChangeListener.change()本身不能让您轻松访问触发事件的源节点。

我通过使用:

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

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

   dosomethingwith(field);

}

});

将字段声明为&#34; final&#34;允许我在ChangeListener.changed()

中访问change事件的源节点
相关问题