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

Enter key does not perform action associated with menu item using ScreenMenuBar

    XMLWordPrintable

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      On Mac OS X with the ScreenMenuBar, navigate menu item by keyboard only: pressing <control> <F2> and arrow keys, then press <enter> key. The action associated with the menu item is not performed.

      If the JMenuItem is a JCheckBoxMenuItem, then it would work on JDK 11.0.12 but not on JDK 16.0.2 (currently latest).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile and run the sample code.
      2. Press <control> <F2> to select the screen menu bar (make sure the check box in System Preferences > Keyboard > Shortcuts > "Move focus to the menu bar" is selected).
      3. Press <right> arrow key twice to select the "File" menu
      4. Press <down> arrow key twice to select the "Menu Item"
      5. Press <enter> key; nothing shows on the console
      6. Repeat steps 2-3 above
      7. Press <down> arrow key three times to select the "Check Box" menu item
      8. Press <enter> key; the word "hello, check box" is shown on the console -- however, this is not working when running with JDK 16

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Step 5: The console should show "hello, menu item"
      Step 8: The console should show "hello, check box"
      ACTUAL -
      Step 4: The console does not show "hello, menu item"
      Step 8: The console shows "hello, check box" on JDK 11 but not JDK 16

      ---------- BEGIN SOURCE ----------
      import java.awt.Desktop;
      import java.awt.event.ActionEvent;

      import javax.swing.AbstractAction;
      import javax.swing.JCheckBoxMenuItem;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.SwingUtilities;

      public class MenuFrame extends JFrame {
          public MenuFrame() {
              JMenuBar menuBar = new JMenuBar();
              JMenu menu = new JMenu("File");
              
              JMenuItem menuItem = menu.add(new AbstractAction("Menu Item") {
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      System.out.println("hello, menu item");
                  }
              });
              
              JMenuItem checkBoxItem = menu.add(new JCheckBoxMenuItem(new AbstractAction("Check Box") {
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      System.out.println("hello, check box");
                  }
              }));
              
              
              menuBar.add(menu);
              setJMenuBar(menuBar);
              
              Desktop.getDesktop().setDefaultMenuBar(menuBar);
          }
          
          public static void main(String...args) {
              SwingUtilities.invokeLater(() -> {
                  MenuFrame editor = new MenuFrame();
                  editor.setSize(400, 300);
                  editor.setVisible(true);
              });
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Have tried various approaches such as using ActionListener, ActionMap/InputMap, ChangeListener, ItemListener, KeyListener, MenuKeyListener, PropertyChangeListener, etc. via the JMenuItem/JMenu/JPopupMenu classes, but no luck.

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              psadhukhan Prasanta Sadhukhan
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated: