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

[macosx] Accelerators such as Command-Z (meta Z) cause menu actions to fire twice

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.7.0_05"
      Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
      Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Mac OS X 10.7.4

      A DESCRIPTION OF THE PROBLEM :
      Perplexing Java 1.7 Swing issue: menu items activated by an accelerator key cause the action listener to get executed twice (it's actually due to a second event in the event queue). This only occurs on Mac, and only when the menu bar is put into the screen menu (Mac-native menu). If the menu bar is in the JFrame, this doesn't occur (also doesn't occur on Windows 1.7 or 1.6 and Mac 1.6).

      Specifically, I have a menu item with an accelerator of "meta Z". When I press "meta Z" (Command-Z), the action is performed twice. "ctrl Z" and "meta Z" both exhibit the problem, while "alt Z" does not.

      The following two stack traces correspond to each call of my action listener. The second one seems to be the new addition as of Java 1.7. The first corresponds to what is seen in Java 1.6.


      java.lang.Exception: Stack trace
      at java.lang.Thread.dumpStack(Thread.java:1342)
      at killer_swing.scratch$_main$fn__505.invoke(scratch.clj:61)
      at killer_swing.swingset$wire_handlers$fn__408.invoke(swingset.clj:214)
      at clojure.lang.AFn.applyToHelper(AFn.java:161)
      at clojure.lang.AFn.applyTo(AFn.java:151)
      at clojure.core$apply.invoke(core.clj:601)
      at killer_swing.swingset$listener$fn__335.invoke(swingset.clj:45)
      at killer_swing.swingset.proxy$java.lang.Object$InvocationHandler$25ae78d5.invoke(Unknown Source)
      at $Proxy5.actionPerformed(Unknown Source)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
      at javax.swing.AbstractButton.doClick(AbstractButton.java:356)
      at javax.swing.plaf.basic.BasicMenuItemUI$Actions.actionPerformed(BasicMenuItemUI.java:802)
      at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1661)
      at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
      at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:689)
      at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:697)
      at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:697)
      at javax.swing.JMenuBar.processKeyBinding(JMenuBar.java:668)
      at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:306)
      at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:292)
      at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2971)
      at javax.swing.SwingUtilities.processKeyBindings(SwingUtilities.java:1585)
      at javax.swing.UIManager$2.postProcessKeyEvent(UIManager.java:1476)
      at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
      at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1017)
      at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:889)
      at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:717)
      at java.awt.Component.dispatchEventImpl(Component.java:4731)
      at java.awt.Container.dispatchEventImpl(Container.java:2287)
      at java.awt.Window.dispatchEventImpl(Window.java:2719)
      at java.awt.Component.dispatchEvent(Component.java:4687)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
      at java.awt.EventQueue.access$000(EventQueue.java:102)
      at java.awt.EventQueue$3.run(EventQueue.java:662)
      at java.awt.EventQueue$3.run(EventQueue.java:660)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.awt.EventQueue$4.run(EventQueue.java:676)
      at java.awt.EventQueue$4.run(EventQueue.java:674)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

      java.lang.Exception: Stack trace
      at java.lang.Thread.dumpStack(Thread.java:1342)
      at killer_swing.scratch$_main$fn__505.invoke(scratch.clj:61)
      at killer_swing.swingset$wire_handlers$fn__408.invoke(swingset.clj:214)
      at clojure.lang.AFn.applyToHelper(AFn.java:161)
      at clojure.lang.AFn.applyTo(AFn.java:151)
      at clojure.core$apply.invoke(core.clj:601)
      at killer_swing.swingset$listener$fn__335.invoke(swingset.clj:45)
      at killer_swing.swingset.proxy$java.lang.Object$InvocationHandler$25ae78d5.invoke(Unknown Source)
      at $Proxy5.actionPerformed(Unknown Source)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
      at com.apple.laf.ScreenMenuItem.actionPerformed(ScreenMenuItem.java:115)
      at java.awt.MenuItem.processActionEvent(MenuItem.java:646)
      at java.awt.MenuItem.processEvent(MenuItem.java:605)
      at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:349)
      at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:337)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:706)
      at java.awt.EventQueue.access$000(EventQueue.java:102)
      at java.awt.EventQueue$3.run(EventQueue.java:662)
      at java.awt.EventQueue$3.run(EventQueue.java:660)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.awt.EventQueue$4.run(EventQueue.java:676)
      at java.awt.EventQueue$4.run(EventQueue.java:674)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the test app attached and press "meta Z" (Command Z). Note that the action is performed twice (action gets printed twice to the console).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Action should be performed once.
      ACTUAL -
      Action is performed twice.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.KeyEvent;

      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.KeyStroke;

      public class ActionFiresTwice {
          public static void main(String[] args) {
              /*
               * Problem does not occur if the following line is commented out.
               */
              System.setProperty("apple.laf.useScreenMenuBar", "true");

              final JFrame f = new JFrame();
              final JMenuBar menuBar = new JMenuBar();
              final JMenu menu = new JMenu("Go");
              final JMenuItem menuItem = menu.add("Undo New");
              /*
               * Problem does not occur if "meta released Z" is used.
               */
              menuItem.setAccelerator(KeyStroke.getKeyStroke("meta Z"));
              menuItem.setActionCommand("DO IT");
              menuItem.addActionListener(new ActionListener() {

                  @Override
                  public void actionPerformed(ActionEvent e) {
                      System.out.println(e);
                  }
              });
              menuBar.add(menu);
              f.setJMenuBar(menuBar);
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.add(new JLabel(System.getProperty("java.version")));
              f.pack();
              f.setVisible(true);
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Using "meta released Z" instead of "meta Z" as the accelerator results in only a single firing. I'm not sure if there are any ill side effects. I haven't seen any, yet.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                leonidr Leonid Romanov (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: