Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8199340

Alert close button is ineffective when custom button is added

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u161, 9.0.4, 10
    • javafx
    • None
    • x86_64
    • windows_10

      An standard Error Alert created with: new Alert(Alert.AlertType.ERROR); can be dismissed by pressing the close button in addition to "OK". However if you add a custom button, like I have in the example below, the close button no longer functions.

      Test case: (attaching the file to this issue failed with "missing token")

      package example;

      import javafx.application.Application;
      import javafx.scene.control.Alert;
      import javafx.scene.control.ButtonType;
      import javafx.scene.control.TextArea;
      import javafx.scene.layout.Priority;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class AlertBug extends Application {

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

      @Override
      public void start(Stage stage) throws Exception {
      showAlertDialog("Alert Close Bug", "The close button doesn't work",
      "When the extra button is added below the close button at the top doesn't work.\n"
      + "Edit the source to turnoff the addition of the \"Copy to clipboard\" button\n"
      + "and the close button up there ^^^ will work."
      );
      }

      private void showAlertDialog(String title, String header, String details) {
      Alert alert = new Alert(Alert.AlertType.ERROR);
      alert.setTitle(title);
      alert.setHeaderText(header);

      // Use a scrollable area for the details
      TextArea textarea = new TextArea();
      textarea.setEditable(false);
      textarea.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
      VBox.setVgrow(textarea, Priority.ALWAYS);
      textarea.setText(details);
      alert.getDialogPane().setContent(textarea);
      alert.setResizable(true);

      // Add a button to copy details to clipboard
      // Set to false and the close button on the Alert starts to work.
      if (true) {
      ButtonType copyTo = new ButtonType("Copy to clipboard");
      alert.getButtonTypes().add(copyTo);
      }

      alert.showAndWait();
      }
      }

            aghaisas Ajit Ghaisas
            swpalmer Scott Palmer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: