JavaFX TextField 侦听器
JavaFX TextField listener
我需要一些帮助。我有一个 ArrayList
个 Textfield
个。
static List<TextField> lfLetters = new ArrayList<>();
我想检查值是否已更改。如果是的话,我想知道它是哪个文本字段。我知道我可以用一个 Listener 来做到这一点,但这只对一个 Listener 有效。
TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
我希望它在数组上工作并确定哪个文本字段已更改。
提前致谢!
我想我会把这个问题标记为重复,因为你有一个完全相似的问题。
但最后,你想在监听器中也有对 TextField
的引用,所以我会添加一个答案。
此代码段将 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);
}
或者如果您的 ArrayList
已经初始化,您可以 iterate through 它只是:
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);
});
});
示例输出
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
您可以将 ObservableList
与适当的提取器一起使用,并将侦听器直接添加到列表中。这样它将自动监视其元素的指定属性的变化。这比为每个文本字段添加侦听器更方便,但在这种情况下您无法获取旧值:
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());
}
}
}
});
我需要一些帮助。我有一个 ArrayList
个 Textfield
个。
static List<TextField> lfLetters = new ArrayList<>();
我想检查值是否已更改。如果是的话,我想知道它是哪个文本字段。我知道我可以用一个 Listener 来做到这一点,但这只对一个 Listener 有效。
TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
我希望它在数组上工作并确定哪个文本字段已更改。
提前致谢!
我想我会把这个问题标记为重复,因为你有一个完全相似的问题
但最后,你想在监听器中也有对 TextField
的引用,所以我会添加一个答案。
此代码段将 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);
}
或者如果您的 ArrayList
已经初始化,您可以 iterate through 它只是:
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);
});
});
示例输出
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
您可以将 ObservableList
与适当的提取器一起使用,并将侦听器直接添加到列表中。这样它将自动监视其元素的指定属性的变化。这比为每个文本字段添加侦听器更方便,但在这种情况下您无法获取旧值:
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());
}
}
}
});