title: How to get the menu item accelerator keys working when using the screen menu bar on OS X
body: <p>On OS X, it makes sense to remove the <code>JMenuBar</code> from your main <code>JFrame</code> and use the screen menu bar instead.</p>
  
  <p>I was under the impression that in recent versions of the Apple JDK this is done using the </p>
  
  <pre><code>Application.getApplication().setDefaultMenuBar( JMenuBar );
  </code></pre>
  
  <p>call.</p>
  
  <p>When using this, it seems like the accelerator keys are no longer working.</p>
  
  <p>The following program illustrates the problem on OS X:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import com.apple.eawt.Application;
  
  public class MacMenuBarShortcutTest {
    public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
          showUI();
        }
      });
    }
  
    private static void showUI(){
      JFrame testFrame = new JFrame("TestFrame");
  
      JLabel content = new JLabel("Press cmd-t to test whether the action is triggered");
      testFrame.getContentPane().add(content);
  
      JMenuBar menuBar = new JMenuBar();
  
      Action action = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
          JOptionPane.showMessageDialog(null, "It works!");
        }
      };
      action.putValue(Action.NAME, "Test action");
      action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_T, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
      JMenu menu = new JMenu("Menu");
      menu.add(new JMenuItem(action));
      menuBar.add(menu);
  
      Application.getApplication().setDefaultMenuBar(menuBar);
  
      testFrame.setVisible(true);
      testFrame.pack();
      testFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
  }
  </code></pre>
  
  <p>The program should allow you to press <kbd>cmd</kbd>+<kbd>t</kbd> and a dialog will pop up, confirming the shortcut for the action works.</p>
  
  <p>This is however not working. The menu item gets highlighted when pressing the short cut, but the action is not executed.</p>
  
  <p>I did not found any relevant methods on the <code>com.apple.eawt.Application</code> class that I forgot to call. Going over the code in a <a href="https://developer.apple.com/library/mac/documentation/Java/Conceptual/Java14Development/07-NativePlatformIntegration/NativePlatformIntegration.html" rel="nofollow">rather outdated article on the Apple website</a> suggested that shortcuts should be working though. </p>
  
  <p>Note that when using </p>
  
  <pre><code>System.setProperty("apple.laf.useScreenMenuBar", "true")
  </code></pre>
  
  <p>instead of the <code>Application#setDefaultMenuBar</code> method, it works as expected. Does this mean the system property is still the recommended way, and I shouldn't be using the method on the <code>Application</code> class ?</p>