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

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

问题描述

我想在我的 JavaFX的TextField 中添加一种侦听器,当用户更改值时在 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的TextField的值更改侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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