United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7160951 [macosx] ActionListener called twice for JMenuItem using ScreenMenuBar
JDK-7160951 : [macosx] ActionListener called twice for JMenuItem using ScreenMenuBar

Details
Type:
Bug
Submit Date:
2012-04-12
Status:
Resolved
Updated Date:
2013-08-08
Project Name:
JDK
Resolved Date:
2012-09-25
Component:
client-libs
OS:
os_x
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:
Backport:
Duplicate:
Relates:
Relates:

Sub Tasks

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
EVALUATION

See evaluation for SubCR
                                     
2012-09-19
URL:   http://hg.openjdk.java.net/jdk8/build/jdk/rev/1ed7fec79bee
User:  katleman
Date:  2012-09-27 06:14:08 +0000

                                     
2012-09-27
Swing_JMenu/Automated/bug7160951
                                     
2013-07-12



Hardware and Software, Engineered to Work Together