I have a TextField https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextField.html and I would like the contents of the field to be restored to their previous value when I press Esc which is expected behaviour on most systems (I have no idea why JavaFX doesn't do this by default).
为此我尝试使用TextField.cancelEdit() https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextInputControl.html#cancelEdit--但似乎这没有做任何事情。
这是一个 SSCCE
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
public class UiTest2Controller extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage aPrimaryStage) throws Exception {
final TextField field = new TextField();
field.setOnAction(aEvent -> {
System.out.println("Action");
});
field.setOnKeyPressed(aEvent -> {
if (aEvent.getCode() == KeyCode.ESCAPE) {
System.out.println("Escape");
field.cancelEdit();
field.getParent().requestFocus();
}
});
field.setPromptText("Hello World...");
aPrimaryStage.setScene(new Scene(new Group(field)));
aPrimaryStage.show();
}
}
重现步骤:
- 在文本字段中输入内容
- Press Esc
预期行为:该字段返回到先前的值(第一次为空)并且焦点丢失。
实际行为:该领域保留了当时的价值,但焦点却消失了。
我不确定具体细节,但如果TextField
has a TextFormatter
分配:
field.setTextFormatter(new TextFormatter<>(TextFormatter.IDENTITY_STRING_CONVERTER));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)