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

[macos] ActionListener called twice when using accelerator key on JCheckBoxMenuItem

XMLWordPrintable

    • x86
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      java version "10.0.2" 2018-07-17
      Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
      Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      An ActionListener that is invoked via an accelerator key is called twice. The same ActionListener invoked via the menu is called once.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run program, use cmd-d

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      modifiers: 0, isSelected: true
      ACTUAL -
      modifiers: 4, isSelected: true
      modifiers: 0, isSelected: false


      ---------- BEGIN SOURCE ----------
      package com.bytezone.swing.bugs;

      import java.awt.EventQueue;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.*;

      public class Bug001 extends JFrame
      {
        final JMenuBar menuBar = new JMenuBar ();
        final JMenu fileMenu = new JMenu ("File");

        final JMenuItem debuggingItem = new JCheckBoxMenuItem ("Debugging");
        // final JMenuItem debuggingItem = new JMenuItem ("Debugging");

        public Bug001 ()
        {
          super ("Bug001");

          menuBar.add (fileMenu);
          fileMenu.add (debuggingItem);
          debuggingItem.setAccelerator (KeyStroke.getKeyStroke ("meta D"));
          setJMenuBar (menuBar);

          debuggingItem.addActionListener (new ActionListener ()
          {
            @Override
            public void actionPerformed (ActionEvent e)
            {
              System.out.printf ("modifiers: %d, isSelected: %s%n", e.getModifiers (),
                  ((JMenuItem) e.getSource ()).isSelected ());
            }
          });
        }

        public static void main (String[] args)
        {
          EventQueue.invokeLater (new Runnable ()
          {
            @Override
            public void run ()
            {
              try
              {
                UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
                System.setProperty ("apple.laf.useScreenMenuBar", "true");
                new Bug001 ().setVisible (true);
              }
              catch (Exception e)
              {
                e.printStackTrace ();
              }
            }
          });
        }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Don't use apple.laf.useScreenMenuBar or don't use JCheckBoxMenuItem.

      FREQUENCY : always


            achung Alisen Chung
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: