JavaFX 文本字段的值更改侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30160899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Value Change Listener for JavaFX's TextField
提问by Elyas Hadizadeh
I would like to add a kind of listener
to my JavaFX's TextField
which when ever a user changes the value of the TextField
, the Application prints something on the console.
我想listener
向我添加一种JavaFX's TextField
,当用户更改 的值时TextField
,应用程序会在控制台上打印一些内容。
I've searched and i find the following very similar question : Value Change Listener to JTextField
我搜索过,发现以下非常相似的问题:Value Change Listener to JTextField
The answer of mentioned question is very clear and efficient, but unfortunately it is only useful for JTextField
( Not JavaFX's TextField
) because it says you should use DocumentListener like this:
上述问题的答案非常清晰有效,但不幸的是,它仅对JTextField
( Not JavaFX's 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();
}
but in JavaFX's TextFields you are not able to do it. So? What is the solution?
但是在 JavaFX 的 TextFields 中,您无法做到。所以?解决办法是什么?
(describing with code can be very good but if it is not possible, any hint will be appreciated)
(用代码描述可能非常好,但如果不可能,任何提示将不胜感激)
采纳答案by Roland
Add a listener to the TextField's textProperty:
为 TextField 的 textProperty 添加一个监听器:
TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});