JavaFX从另一个打开一个对话框

时间:2015-09-17 16:55:46

标签: java javafx dialog alert

我的应用程序中有以下工作流导致问题:

单击按钮以打开对话框>打开对话框>单击对话框中的按钮>显示确认提示>确认后,关闭第一个对话框并打开一个新对话框

第二个对话框不允许输入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;
  }
}

3 个答案:

答案 0 :(得分:8)

问题

如上所述,你有一个模态问题。

解决方案

以下代码将演示一个解决方案,询问用户是否真的要打印,打印后,如果结束编号是正确的。

注意,我使用来自here的类IntField)

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.Window;

public class DialogTest extends Application {

    Region veil;
    ProgressIndicator indicator;

    IntField startingNumber = new IntField(0, 999999, 0);
    IntField endingNumber = new IntField(startingNumber.getValue(), 999999, startingNumber.getValue() + 1);
    ButtonType printButtonType = new ButtonType("Print", ButtonData.OK_DONE);
    Stage stage;

    @Override
    public void start(Stage primaryStage) {
        stage = primaryStage;
        Button button = new Button("Print Checks");

        VBox box = new VBox(10, button);
        box.setAlignment(Pos.CENTER);

        veil = new Region();
        veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.3);");
        veil.setVisible(false);

        indicator = new ProgressIndicator();
        indicator.setMaxHeight(60);
        indicator.setMinWidth(60);
        indicator.setVisible(false);

        StackPane root = new StackPane(box, veil, indicator);

        root.setAlignment(Pos.CENTER);

        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();

        button.setOnAction((event) -> {
            Dialog<ButtonType> dialog
                    = getCheckPrintDialog(primaryStage, "Enter starting check number");
            dialog.showAndWait()
                    .filter(result -> result == printButtonType)
                    .ifPresent(result -> {
                        // this is for this example only, normaly you already have this value
                        endingNumber.setValue(startingNumber.getValue() + 1);
                        printChecks(startingNumber.getValue(), endingNumber.getValue());
                    });
        });
    }

    public static void main(String[] args) {
        launch(args);
    }

    public <R extends ButtonType> Dialog getCheckPrintDialog(Window owner, String title) {
        Dialog<R> dialog = new Dialog<>();
        dialog.initOwner(owner);
        dialog.setTitle(title);
        dialog.getDialogPane().getButtonTypes().addAll(printButtonType, ButtonType.CANCEL);

        Button btOk = (Button) dialog.getDialogPane().lookupButton(printButtonType);
        btOk.addEventFilter(ActionEvent.ACTION, event -> {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Print Checks? Are you sure?", ButtonType.YES, ButtonType.NO);
            alert.showAndWait()
                    .filter(result -> result == ButtonType.NO)
                    .ifPresent(result -> event.consume());
        });

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);

        Text from = new Text("Starting Number:");
        grid.add(from, 0, 0);

        grid.add(startingNumber, 1, 0);

        dialog.getDialogPane().setContent(grid);
        return dialog;
    }

    private void printChecks(int from, int to) {

        Task<Void> task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                Thread.sleep(5000);
                return null;
            }
        };

        task.setOnSucceeded((event) -> {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Has the last check, the number: " + endingNumber.getValue() + "?", ButtonType.YES, ButtonType.NO);
            alert.initOwner(stage);
            Button btnNo = (Button) alert.getDialogPane().lookupButton(ButtonType.NO);
            btnNo.addEventFilter(ActionEvent.ACTION, e -> {
                Dialog<ButtonType> newEndNum = new Dialog<>();
                newEndNum.setTitle("Enter the ending check number");
                newEndNum.initOwner(stage);
                newEndNum.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
                GridPane grid = new GridPane();
                grid.setHgap(10);
                grid.setVgap(10);

                Text toUser = new Text("Ending Number:");
                grid.add(toUser, 0, 0);

                grid.add(endingNumber, 1, 0);

                newEndNum.getDialogPane().setContent(grid);
                newEndNum.showAndWait().filter(result -> result == ButtonType.CANCEL)
                        .ifPresent(result -> e.consume());
            });
            alert.showAndWait();
        });
        veil.visibleProperty().bind(task.runningProperty());
        indicator.visibleProperty().bind(task.runningProperty());
        new Thread(task).start();
    }

}

工作申请

  1. 主窗口:
  2. Main

    1. 打印对话框:
    2. PrintDialog

      1. 点击“打印”(警报已本地化,对我来说是德语):
      2. Confirm

        1. 点击“是”后,“打印对话框”关闭,进度将显示(本例中为5秒)
        2. Progress

          1. 打印完成后出现一个对话框,要求输入正确的结尾编号
          2. Confirm2

            1. 如果单击是全部已完成,如果单击否,则打开另一个对话框以输入正确的结束值
            2. EndingNumber

答案 1 :(得分:4)

我找到了问题所在。但是因为我刚开始使用JavaFx,所以无法提供“为什么”。 在我看来,问题出在 dialog.close(),就在 if(alert.showAndWait()。get()== ButtonType.YES)之后。 当你关闭它或类似的东西时,看起来它会丢失一些对话框的引用(我让专家清楚这一点)。

作为一种解决方法,它适用于我,将 dialog.close()移到 dialog2.showAndWait();

之后
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(); // supressed this and placed at the bottom
                    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();
                    dialog.close(); // new location
                }
            });
        }

        return dialog;
    }

发生这种情况的原因我无法解释,但这可能是一种解决方法。 我希望这会对你有所帮助。

答案 2 :(得分:2)

在你的&#34;开始&#34;创建Dialog1的方法,你应该调用dialog.show()而不是dialog.showAndWait()。

    button.setOnAction(event -> {
        Dialog<Pair<String, String>> dialog = getDialog(scene.getWindow(), "Dialog 1", true);
        // dialog.showAndWait();
        dialog.show();
    });