JavaFX 文本字段的值更改侦听器 [英] Value Change Listener for JavaFX's TextField

查看:35
本文介绍了JavaFX 文本字段的值更改侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的 JavaFX 的 TextField 添加一种 listener,当用户更改 TextField 的值时,应用程序在控制台上打印一些东西.

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.

我搜索过,发现以下非常相似的问题:值更改监听器到 JTextField

I've searched and i find the following very similar question : Value Change Listener to JTextField

上述问题的答案非常清晰有效,但不幸的是它只对 JTextField(不是 JavaFX 的 TextField)有用,因为它说你应该像这样使用 DocumentListener:

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:

// 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 的 TextFields 中你不能这样做.所以?有什么解决办法?

but in JavaFX's TextFields you are not able to do it. So? What is the solution?

(用代码描述可以很好,但如果不可能,任何提示将不胜感激)

(describing with code can be very good but if it is not possible, any hint will be appreciated)

推荐答案

为 TextField 的 textProperty 添加一个监听器:

Add a listener to the TextField's textProperty:

TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
    System.out.println("textfield changed from " + oldValue + " to " + newValue);
});

这篇关于JavaFX 文本字段的值更改侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆