我想检测文本字段中的字符串何时发生变化(添加字符时和退格字符时),为此我尝试使用 onKeyTyped 函数
SearchTF.onKeyTypedProperty(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
此代码不起作用
这是我的三个问题
- 为什么这不起作用
- 我怎样才能让它工作
- 是否有更好的方法来检测文本字段中的字符串何时发生变化? (如第一行所述)
最佳答案
myTextField.textProperty().addListener((object, oldValue, newValue) -> {
if (newValue.length() > oldValue.length()) {
System.out.println("letter add");
} else {
System.out.println("letter removed");
}
});
关于java - 如何在javaFX中使用setOnKeyTyped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61306081/