This discussion is archived
2 Replies Latest reply: Jul 26, 2013 2:29 AM by PhHein RSS

com.apple.eawt.Application.setDefaultMenuBar() and accelerator problem

820145 Newbie
Currently Being Moderated
Has anyone else come across this? I've googled, and searched here, but nothing comes up. Kinda hard to believe I'm the first to stumble across it, but whatever.

I have a JMenuBar with a single menu in it, containing a single menu item. There is an accelerator assigned for it (Command-S) and the menu item is implemented by a descendant of AbstractAction. I then call com.apple.eawt.Application.setDefaultMenuBar(), passing it the menu bar instance.

When I run the program, if I choose the menu item then the item's action is called and all is well. If I try using Command-S, nothing happens. The action is not called.

Here is an SSCCE which demonstrates the problem:
public class MenuTrial extends JPanel
{
  public static void main(String[] args)
  {
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        JFrame frame = new JFrame("MenuTrial");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MenuTrial newContentPane = new MenuTrial();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem menuItem = new JMenuItem(new AbstractAction()
        {

          public void actionPerformed(ActionEvent actionEvent)
          {
            System.out.println("hello world");
          }
        });
        menuItem.setText("Save");
        menuItem.setAccelerator(
          KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.META_MASK));
        fileMenu.add(menuItem);
        menuBar.add(fileMenu);

        com.apple.eawt.Application theApp =
          com.apple.eawt.Application.getApplication();



/***************Comment out one of the following two lines */
        theApp.setDefaultMenuBar(menuBar); // this line causes the problem
//        frame.setJMenuBar(menuBar); // this line works perfectly



        frame.pack();
        frame.setVisible(true);
      }
    });
  }
}
If I comment out the call to setDefaultMenuBar() and activate the line below it (frame.setJMenuBar(menuBar)), all is well. At runtime Command-S results in the action being called.

Does anyone have any wisdom to shine on this issue? Someone must surely have hit it before me! I know that this dates from, at the earliest, the middle of this year, since the Java which I had at that stage didn't have the "shiny, new" setDefaultMenuBar(), and the version of my app which I built back then has working accelerator keys. Since then I have upgraded my Java at least once (possibly twice...memory not as infallible as it used to be... :( )

Any help gratefully received! :)

Java version 1.6.0_22 (build 1.6.0_22-b04-307-10M3261) on a MacBook Pro running 10.6.5.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points