I have the problem that the accelerators of JMenuItems aren't working anymore after showing two JDialogs directly after one another.
Please take a look at this small example that reproduces the problem:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DialogBug
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new StartupRunnable(args.length == 0));
}
public static class StartupRunnable
implements Runnable
{
private boolean both;
public StartupRunnable(boolean both)
{
this.both=both;
}
public void run()
{
MyFrame myFrame=new MyFrame();
myFrame.setVisible(true);
myFrame.startUp(both);
}
}
public static class MyFrame
extends JFrame
{
private MyDialog dialog1;
private MyDialog dialog2;
public MyFrame()
{
super("MyFrame");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initUI();
}
private void initUI()
{
dialog1=new MyDialog(this);
dialog2=new MyDialog(this);
JMenuBar menuBar=new JMenuBar();
JMenu fileMenu=new JMenu("File");
menuBar.add(fileMenu);
fileMenu.add(new JMenuItem(new OpenAction()));
setJMenuBar(menuBar);
setSize(200,200);
}
public void startUp(boolean both)
{
dialog1.setVisible(true);
if(both)
{
dialog2.setVisible(true);
}
}
private class OpenAction
extends AbstractAction
{
public OpenAction()
{
super("Open");
KeyStroke accelerator = KeyStroke.getKeyStroke("ctrl O");
putValue(Action.ACCELERATOR_KEY, accelerator);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Open executed");
}
}
}
public static class MyDialog
extends JDialog
{
public MyDialog(JFrame parent)
{
super(parent);
setTitle("Dialog");
setModal(false);
add(new JButton(new OkAction()));
pack();
}
private class OkAction
extends AbstractAction
{
public OkAction()
{
super("Ok");
}
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
}
}
}
Compile it using javac DialogBug.java
and execute it running java DialogBug
.
You'll see two with "Ok"-Buttons. Dismiss both dialogs clicking them. Now press "Ctrl-O". This should print "Open executed" to the console but this won't happen. Now click the "File" menu. Now "Ctrl-O" will work as expected.
If you start the app with any argument, e.g. java DialogBug x
then only one dialog will open and "Ctrl-O" will work immediately after dismissing the dialog, as expected.
My environment is the following:
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03-219)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02-90, mixed mode)
But as far as I know this app behaves similar in other VMs.
Please help me!
I really don't have any idea what might go wrong here. If this is a Java bug (and I expect it to be one) then please let me know if you have a workaround...
If you can or can't reproduce this on other systems please tell me so in the comments.
Thanks a lot!
Update
After installing Java 1.5 on Snow Leopard (sigh) I can confirm that this happens with 1.5.0_19, too, at least on Snow Leopard.
Update 2
Works for me on Windows XP.
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05)
Java HotSpot(TM) Client VM (build 1.5.0_13-b05, mixed mode)
Update 3
Seems to work on Windows Vista, too. This makes this a Mac OS X 10.5+10.6 issue, AFAIK so far.
Update 4
This bug is filed under Problem ID #7240026 at Apple.