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

[macosx] Action registered for keyboard shortcut is called twice

    XMLWordPrintable

Details

    • b100
    • x86
    • os_x

    Description

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)

      ADDITIONAL OS VERSION INFORMATION :
      OSX El Capitan v 10.11

      A DESCRIPTION OF THE PROBLEM :
      I have a swing action with an accelerator like "meta shift COMMA". I put it in the component's action map, when I invoke it, the action is called twice.

      ADDITIONAL REGRESSION INFORMATION:
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a swing action, set on it as accelerator "meta shift COMMA", add it in the input map of a component, trigger it and it will get called twice.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The action should only get called once.
      ACTUAL -
      The action gets triggered twice.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package ro.sync;

      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;

      import javax.swing.AbstractAction;
      import javax.swing.Action;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.KeyStroke;
      import javax.swing.text.BadLocationException;

      /**
       * @author mircea
       */
      public class TestFrame {

        /**
         * @param args
         */
        public static void main(String[] args) {
          System.setProperty("com.apple.mrj.application.apple.menu.about.name", "A test frame");
          System.setProperty("apple.laf.useScreenMenuBar", "true");
          // This is in case of Java 1.3 on Mac OS X.
          System.setProperty("com.apple.macos.useScreenMenuBar", "true");
          
          JFrame frame = new JFrame("Test frame !");
          
          JPanel content = new JPanel(new BorderLayout());
          
          final JTextArea textArea = new JTextArea();
          content.add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
          
          JMenuBar menuBar = new JMenuBar();
          frame.setJMenuBar(menuBar);
          
          Action a = new AbstractAction("Insert some text...") {
            @Override
            public void actionPerformed(ActionEvent arg0) {
              try {
                textArea.getDocument().insertString(0, "Some text... ", null);
              } catch (BadLocationException e) {
                e.printStackTrace();
              }
            }
          };
          KeyStroke keyStroke = KeyStroke.getKeyStroke("meta shift COMMA");
          a.putValue(Action.ACCELERATOR_KEY, keyStroke);
          
          textArea.getInputMap().put(keyStroke, "myAction");
          
          textArea.getActionMap().put("myAction", a);
          JMenu menu = new JMenu("The Menu");
          
          JMenuItem mi = new JMenuItem(a);
          mi.setAccelerator((KeyStroke) a.getValue(Action.ACCELERATOR_KEY));
          menu.add(mi);
          menuBar.add(menu);
          
          frame.getContentPane().add(content);
          
          frame.setLocation(200, 100);
          frame.setSize(500, 500);
          
          frame.setVisible(true);
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use another shortcut for the action. Not all shortcuts lead to this problem.

      Attachments

        Issue Links

          Activity

            People

              aniyogi Avik Niyogi (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: