【发布时间】:2015-12-14 15:39:50
【问题描述】:
我的应用程序中有以下工作流程导致问题:
单击按钮打开对话框 > 打开对话框 > 单击对话框中的按钮 > 显示确认警报 > 确认后关闭第一个对话框并打开一个新对话框
第二个对话框不允许输入到 TextField。我已经包含了一个显示问题的 SSCE。另外一件奇怪的事情是,如果您尝试通过单击“X”关闭第二个对话框,然后关闭警报,那么我就可以在该字段中输入。
public class DialogTest extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Show Dialog");
VBox root = new VBox(10, button);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 350, 120);
primaryStage.setScene(scene);
primaryStage.show();
button.setOnAction(event -> {
Dialog<Pair<String, String>> dialog = getDialog(scene.getWindow(), "Dialog 1", true);
dialog.showAndWait();
});
}
public static void main(String[] args) {
launch(args);
}
public Dialog<Pair<String, String>> getDialog(Window owner, String title, boolean addButton) {
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle(title);
dialog.initOwner(owner);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
if(addButton) {
Button button = new Button("Show Dialog");
dialog.getDialogPane().setContent(button);
button.setOnAction(event -> {
Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure?", ButtonType.YES, ButtonType.NO);
alert.initOwner(owner);
if(alert.showAndWait().get() == ButtonType.YES) {
dialog.close();
Dialog<Pair<String, String>> dialog2 = getDialog(owner, "Dialog 2", false);
TextField textField = new TextField();
dialog2.getDialogPane().setContent(textField);
dialog2.getDialogPane().getScene().getWindow().setOnCloseRequest(closeEvent -> {
closeEvent.consume();
if(textField.getText().trim().isEmpty()) {
Alert alert2 = new Alert(AlertType.ERROR, "Please enter a value", ButtonType.OK);
alert2.initOwner(dialog2.getDialogPane().getScene().getWindow());
alert2.showAndWait();
}
});
dialog2.showAndWait();
}
});
}
return dialog;
}
}
【问题讨论】:
-
我没有注意到任何奇怪的行为,除了第二个对话框上的 TextField 没有显示插入的文本(但你可以在那里输入,并且 getText() 正在返回正确的字符串)。我无法确定此 TextField 错误的原因。单击 X 按钮后,第二个对话框不会关闭,因为您使用了此事件。如果您在文本字段中插入了一些文本(您看不到),最后的警报将不会显示
-
@tomasz77 是的,文本未显示在 TextField 中的问题是我正在谈论的问题。虽然单击 X 然后关闭显示“请输入一个值”的对话框,但我可以在该字段中输入。