JavaFX'的值更改侦听器;s文本字段
我想在我的JavaFX'的值更改侦听器;s文本字段,java,javafx,javafx-8,documentlistener,Java,Javafx,Javafx 8,Documentlistener,我想在我的JavaFX的TextField中添加一种listener,当用户更改TextField的值时,应用程序会在控制台上打印一些内容 我搜索了一下,发现了以下非常相似的问题: 上述问题的答案非常清晰有效,但不幸的是,它只对JTextField(而不是JavaFX的TextField)有用,因为它说您应该像这样使用DocumentListener: // Listen for changes in the text textField.getDocument().addDocumentLis
JavaFX的TextField
中添加一种listener
,当用户更改TextField
的值时,应用程序会在控制台上打印一些内容
我搜索了一下,发现了以下非常相似的问题:
上述问题的答案非常清晰有效,但不幸的是,它只对JTextField
(而不是JavaFX的TextField
)有用,因为它说您应该像这样使用DocumentListener:
// Listen for changes in the text
textField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
warn();
}
public void removeUpdate(DocumentEvent e) {
warn();
}
public void insertUpdate(DocumentEvent e) {
warn();
}
但在JavaFX的文本字段中,您无法执行此操作。
所以解决办法是什么
(使用代码进行描述可能非常好,但如果不可能,请提供任何提示)将侦听器添加到TextField的textProperty:
TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
这实际上会使textfield失去焦点,这对用户来说是非常令人沮丧的。对于使用Kotlin的用户来说,上面的代码片段如下:
val textfield=textfield()
textfield.textProperty().addListener{observable,oldValue,newValue->println(“textfield从$oldValue更改为$newValue”)}
在新线程(()->{Platform.runLater(()->{)中设置焦点。这可能是一个bug。