简体   繁体   English

JavaFX TextField侦听器

[英]JavaFX TextField listener

I need some help. 我需要协助。 I have an ArrayList of Textfield s. 我有一个TextfieldArrayList

 static List<TextField> lfLetters = new ArrayList<>();

And I want to check if the value has changed. 我想检查值是否已更改。 And if it was I want to know which textfield it was. 如果是这样,我想知道它是哪个文本字段。 I know I can do this with a Listener but that only worked for a single one. 我知道我可以用一个监听器做到这一点,但这只适用于一个人。

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

I want it to work on an Array and determine which textfield has changed. 我想让它在数组上工作并确定哪个文本字段已更改。

Thanks in advance! 提前致谢!

You can use ObservableList with appropriate extractor and add listener directly to list. 您可以将ObservableList与适当的提取器一起使用,并将侦听器直接添加到列表中。 This way it will be automatically watching changes in the specified properties of its elements. 这样它将自动观察其元素的指定属性的变化。 It is more convenient, than adding listener to each text field, but in this case you can't get old value: 比向每个文本字段添加监听器更方便,但在这种情况下,您无法获得旧值:

    ObservableList<TextField> oList = 
        FXCollections.observableArrayList(tf -> new Observable[]{tf.textProperty()});  

    oList.addListener((ListChangeListener.Change<? extends TextField> c) -> {
        while (c.next()) {
            if (c.wasUpdated()) {
                for (int i = c.getFrom(); i < c.getTo(); ++i) {
                    System.out.println("Updated index: " + i + ", new value: " + c.getList().get(i).getText());
                }
            }
        }
    });

I was thinking that I will mark this quesiton as a duplicate, as you have a totally similar question here . 我想我会把这个问题标记为副本,因为你在这里有一个完全相似的问题。

But in the end, you want to have the reference to the TextField also in the listener, so I will add an answer. 但最后,您希望在侦听器中也引用TextField ,因此我将添加一个答案。

This snippet adds 10 TextField objects to the ArrayList and adds a listener to each. 此代码段将10个TextField对象添加到ArrayList并为每个对象添加一个侦听器。

for (int i = 0; i < 10; i++) {
    TextField tf = new TextField();
    final int index = i;
    tf.textProperty().addListener((obs, oldVal, newVal) -> {
        System.out.println("Text of Textfield on index " + index + " changed from " + oldVal
                + " to " + newVal);
    });
    lfLetters.add(tf);
}

Or if your ArrayList is already initialized, you can iterate through it simply: 或者,如果您的ArrayList已经初始化,您可以简单地遍历它:

lfLetters.forEach(tf -> {
    tf.textProperty().addListener((obs, oldVal, newVal) -> {
        System.out.println("Text of Textfield on index " + lfLetters.indexOf(tf) + " changed from " + oldVal
                + " to " + newVal);
    });
});

Sample output 样本输出

Text of Textfield on index 2 changed from InitialText - 2 to ModifiedText - 2
Text of Textfield on index 6 changed from InitialText - 6 to ModifiedText - 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM