在JavaFX上单击两次警报确认对话框

时间:2015-08-12 10:21:41

标签: javafx click alertdialog

我在javafx应用程序上有一个对话框警报。当我点击任何按钮(是或否)时,我必须点击两次才能执行每个按钮的操作。我只想点击一下!有人能帮帮我吗?

创建提醒的方法:

public class AlertMessages {
    public static boolean alertConfirmation(String header, String text) {
        Alert dialog = new Alert(Alert.AlertType.CONFIRMATION);
        dialog.setHeaderText(header);
        dialog.setContentText(text);
        dialog.setResizable(true);
        dialog.getDialogPane().setPrefSize(350, 200);
        dialog.showAndWait();
        final Optional<ButtonType> result = dialog.showAndWait();
        return result.get() == ButtonType.OK;
    }
}

调用方法:

public class OverviewController {
     ...
     ...

     @FXML
     private void handleButton() {
          ...
          else if (...) {
               header = "...";
               text = "...";
               boolean res = AlertMessages.alertConfirmation(header, text);
               if (res) {
                   ...
               } else {
                   ...
               }
          ...
     }

     ...
     ...
}

非常感谢。 问候。

1 个答案:

答案 0 :(得分:1)

您正在拨打dialog.showAndWait()两次:

dialog.showAndWait();
final Optional<ButtonType> result = dialog.showAndWait();

只需删除第一个电话。