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

ContextMenu and MenuBar can't add the same MenuItem at the same time

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 8u25
    • Fix Version/s: tbd
    • Component/s: javafx
    • Environment:

      OS: Windows 7
      JavaFX: Java 8u25

      Description

      ContextMenu and MenuBar can't add the same MenuItem at the same time, for example:

      public class KeyCombinationsAndContextMenus extends Application {
          
          @Override
          public void start(Stage primaryStage) {
              BorderPane root = new BorderPane();
              Scene scene = new Scene(root, 530, 350, Color.WHITE);

              final StringProperty statusProperty = new SimpleStringProperty();

              InnerShadow iShadow = new InnerShadow();
              iShadow.setOffsetX(3.5f);
              iShadow.setOffsetY(3.5f);
              final Text status = new Text();
              status.setEffect(iShadow);
              status.setX(100);
              status.setY(50);
              status.setFill(Color.LIME);
              status.setFont(Font.font(null, FontWeight.BOLD, 35));
              //status.setTranslateY(50);

              status.textProperty().bind(statusProperty);
              statusProperty.set("Keyboard Shortcuts\n"
                      + "Ctrl-N, \n"
                      + "Ctrl-S, \n"
                      + "Ctrl-X \n"
                      + "Ctrl-Shift-E");
              root.setCenter(status);

              MenuBar menuBar = new MenuBar();
              menuBar.prefWidthProperty()
                     .bind(primaryStage.widthProperty());
              root.setTop(menuBar);

              Menu fileMenu = new Menu("_File");
              fileMenu.setMnemonicParsing(true);
              menuBar.getMenus().add(fileMenu);

              MenuItem newItem = new MenuItem("_New");
              newItem.setMnemonicParsing(true);

              newItem.setAccelerator(new KeyCodeCombination(KeyCode.N,
                      KeyCombination.SHORTCUT_DOWN));
              newItem.setOnAction(actionEvent -> statusProperty.set("Ctrl-N"));
              fileMenu.getItems().add(newItem);

              MenuItem saveItem = new MenuItem("_Save");
              saveItem.setMnemonicParsing(true);
              saveItem.setAccelerator(new KeyCodeCombination(KeyCode.S,
                      KeyCombination.SHORTCUT_DOWN));
              saveItem.setOnAction(actionEvent -> statusProperty.set("Ctrl-S"));
              fileMenu.getItems().add(saveItem);

              fileMenu.getItems().add(new SeparatorMenuItem());

              MenuItem exitItem = new MenuItem("Exit");
              exitItem.setAccelerator(new KeyCodeCombination(KeyCode.X,
                      KeyCombination.SHORTCUT_DOWN));

              exitItem.setOnAction(actionEvent -> {
                  statusProperty.set("Ctrl-X");
                  Platform.exit();
              });

              scene.getAccelerators().put(
                      new KeyCodeCombination(KeyCode.E,
                                             KeyCombination.SHORTCUT_DOWN,
                                             KeyCombination.SHIFT_DOWN),
                      () -> statusProperty.set("Ctrl-Shift-E")
              );

              fileMenu.getItems().add(exitItem);

              //exitItem = new MenuItem("Exit"); // must create a new one
              ContextMenu contextFileMenu = new ContextMenu(exitItem);

              primaryStage.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me) -> {
                  if (me.getButton() == MouseButton.SECONDARY) {
                      contextFileMenu.show(root, me.getScreenX(), me.getScreenY());
                  } else {
                      contextFileMenu.hide();
                  }
              });


              primaryStage.setScene(scene);
              primaryStage.show();
          }

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

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              duke J. Duke (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported: