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

[macOS] Accelerator assigned to button in dialog fires menuItem in owning stage



    • x86_64
    • os_x



        System Version: macOS 10.13.5 (17F77)
        Kernel Version: Darwin 17.6.0

        $java -version
        java version "1.8.0_172"
        Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
        Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

        A main window of a Java FX application has a "File > Save" menu item with Command-S as its accelerator. A "preview" dialog is opened, and it has a button with text, "Save to PDF". A "Command-S" accelerator and an eventFilter are registered, either with the button, or the dialog window, so that the user can use the keyboard to save the preview to PDF.

        On OS X, when the user presses Command-S in this preview dialog, the generated event leaks back to the owning stage, and fires the "File > Save" menu item. It also fires the "Save to PDF" button, but the fact that the owning stage receives the event and handles it is not correct behavior.

        I have tested on OS X High Sierra and Windows 10. The latter behaves correctly. So, as far as I can tell, the issue is specific to OS X HS (10.13.5).

        See the attached code.

        EXPECTED -
        Pressing an accelerator in a dialog to fire a button should not result in the firing of a control (button/menu item) assigned to the same accelerator in the owning window.
        ACTUAL -
        Pressing an accelerator in a dialog to fire a button in that dialog actually fires a control (button/menu item) assigned to the same accelerator in the owning window, as well as the button in the dialog.

        ---------- BEGIN SOURCE ----------
        package show.the.bug;

        import java.util.Optional;

        import org.slf4j.Logger;
        import org.slf4j.LoggerFactory;

        import javafx.application.Application;
        import javafx.application.Platform;
        import javafx.event.ActionEvent;
        import javafx.geometry.Insets;
        import javafx.geometry.Pos;
        import javafx.scene.Scene;
        import javafx.scene.control.Alert;
        import javafx.scene.control.Alert.AlertType;
        import javafx.scene.control.Button;
        import javafx.scene.control.ButtonBar.ButtonData;
        import javafx.scene.control.ButtonType;
        import javafx.scene.control.Label;
        import javafx.scene.control.Menu;
        import javafx.scene.control.MenuBar;
        import javafx.scene.control.MenuItem;
        import javafx.scene.input.KeyCode;
        import javafx.scene.input.KeyCodeCombination;
        import javafx.scene.input.KeyCombination;
        import javafx.scene.layout.Pane;
        import javafx.scene.layout.VBox;
        import javafx.stage.Modality;
        import javafx.stage.Stage;
        import javafx.stage.Window;

        public class Main extends Application {

            private static final Logger log = LoggerFactory.getLogger(Main.class);
            private Label label;
            private Stage stage;
            private MenuItem saveMenuItem;

            final private String showPdfPreviewText = "Show PDF Preview";

            public static void main(String[] args) {

            public void start(Stage primaryStage) {
                stage = primaryStage;
                stage.setTitle("Accelerator Generated Events Leak From Dialog!");

                final Button button = new Button(showPdfPreviewText);
                final String ifThisChangesText = "If this text changes after pressing Command-S/Control-S "
                    + "in the \"Show PDF Preview\" dialog, there's a problem! It will indicate that File > Save was fired.";
                label = new Label(ifThisChangesText);

                button.setOnAction(actionEvent -> {

                final VBox vBox = new VBox(button, label);
                VBox.setMargin(vBox, new Insets(12));

                final Scene scene = new Scene(new VBox(vBox), 400, 250);
                ((Pane) scene.getRoot()).getChildren().add(0, configureMenuBar());

            private MenuBar configureMenuBar() {
                MenuBar menuBar = new MenuBar();
                Menu fileMenu = new Menu("File");
                saveMenuItem = new MenuItem("Save");
                saveMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN));
                saveMenuItem.setOnAction(ae -> {
                    log.debug("saveTtem fired: {}", ae);
                    Platform.runLater(() -> label.setText(
                        "Whoa! Not good.\n\n"
                            + "Save menu item action was fired from the \"PDF Preview\" dialog.\n\n"
                            + "Events should not leak from a dialog back to the owning stage/window."));
                return menuBar;

            private void showAlert(final Window owner) {
                final ButtonType saveButtonType = new ButtonType("Save to PDF...", ButtonData.LEFT);
                final Alert previewAlert = new Alert(
                    "Just some text. Press Command-S to test whether expected behavior occurs.",

                previewAlert.setTitle("PDF Preview");
                previewAlert.setContentText("\n\nLorem ipsum dolor sit amet, consetetur sadipscing elitr, ...\n\n"
                    + "Hit Command-S (Mac), Control-S (Windows) to show the problem.\n\n");
                // The result is the same with Modality.APPLICATION_MODAL

                final Button saveButton = (Button) previewAlert.getDialogPane().lookupButton(saveButtonType);

                 * Configure necessary event filter for the alert/dialog. Add SHORTCUT_DOWN+S to fire the saveButton.
                saveButton.sceneProperty().addListener((obs, oldScene, newScene) -> {
                    if (oldScene == null && newScene != null) {
                        newScene.getWindow().addEventFilter(ActionEvent.ACTION, ae -> {
                            log.debug("event filter caught actionEvent: {}", ae);
                            Platform.runLater(() -> {
                                // Using an alert for convenience. Dialogs are essentially the same.
                                Alert saveAlert = new Alert(AlertType.INFORMATION, "This is the \"Save as PDF...\" dialog.\n\n"
                                    + "In a real app, it would be a FileChooser.");
                                saveAlert.setTitle("Save as PDF");
                                saveAlert.setHeaderText("Save as PDF...");
                                    "See the work-around involving disabling the saveMenuItem "
                                    + "before showing the \"PDF Preview\" dialog.");
                        log.debug("setting up saveButton: {}", newScene);
                            new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),

                 * To work around the leaking event, disable the saveMenuItem prior to showing the
                 * alert/dialog. Then re-enable it after the dialog has been closed. Uncomment
                 * the next two commented lines and run again to see the effect.
        // saveMenuItem.setDisable(true);
                final Optional<ButtonType> result = previewAlert.showAndWait();
        // saveMenuItem.setDisable(false);

                log.debug("result from Command-S: {}", result);
                    .filter(bt -> ButtonData.LEFT.equals(bt.getButtonData()))
                    .ifPresent(bt -> log.debug("alert result: {}", bt));

             * Reposition the alert so that it doesn't cover the text shown in the main window. That text
             * tells you what to expect and what actually happens.
             * @param alert
            protected void positionAlert(Alert alert) {
                alert.setOnShown(ev -> {
                    alert.setY(stage.getY() + stage.getHeight() + 20.0);


        ---------- END SOURCE ----------

        See the attached code, specifically near the bottom of the file where the comment tells you to uncomment two lines surrounding the "showAndWait()" invocation.

        FREQUENCY : always


          Issue Links



                mfox Martin Fox
                webbuggrp Webbug Group
                1 Vote for this issue
                7 Start watching this issue