JDK-7160951 : [macosx] ActionListener called twice for JMenuItem using ScreenMenuBar
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2012-04-12
  • Updated: 2016-03-31
  • Resolved: 2012-09-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u10Fixed 8 b58Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_04-ea"
Java(TM) SE Runtime Environment (build 1.7.0_04-ea-b19)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b20, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Mac OS X 10.7.3

A DESCRIPTION OF THE PROBLEM :
When the ScreenMenuBar is activated using  System.setProperty("apple.laf.useScreenMenuBar", "true"); and the registered Accelerator Key for a JMenuItem is pressed, the ActionListener is called twice.


REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Activate the usage of the Screen Menu Bar
System.setProperty("apple.laf.useScreenMenuBar", "true");

Create a JFrame with a JMenuBar, JMenu and a JMenuItem and register a hotkey to the JMenuItem using setAccelerator and add an ActionListener.

Start the test application and press the Hotkey.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Having the ActionListener called once per pressed hotkey.
ACTUAL -
The ActionListener its called twice.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

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

public class Test extends JFrame {

	public Test() {
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenuItem openItem = new JMenuItem("Open");
		openItem.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		openItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("i was called");
			}
		});
		fileMenu.add(openItem);
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);
		pack();
		setVisible(true);
	}

	public static void main(String[] args) throws InvocationTargetException, InterruptedException {
		System.setProperty("apple.laf.useScreenMenuBar", "true");
		SwingUtilities.invokeAndWait(new Runnable() {

			@Override
			public void run() {
				new Test();
			}
		});
	}
}

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

CUSTOMER SUBMITTED WORKAROUND :
Do not use the ScreenMenuBar.

Comments
Swing_JMenu/Automated/bug7160951
12-07-2013

EVALUATION See evaluation for SubCR
19-09-2012