JMenuItem keyboard accelerator not showing when using the useScreenMenuBar property
Marco Dinacci
marco.dinacci at gmail.com
Mon Jun 18 08:54:24 PDT 2012
Hi Leonid,
I think I've found the issue. In ScreenMenuItem.addNotify there's the
following code:
setAccelerator(fMenuItem.getAccelerator());
final String label = fMenuItem.getText();
if (label != null) {
setLabel(label);
}
setLabel(label) in CMenuItem calls:
setLabel(label, (char)0, KeyEvent.VK_UNDEFINED, 0);
which resets any accelerator previously set with setAccelerator.
If the call to setAccelerator(fMenuItem.getAccelerator()) is placed
after setLabel(label) than the accelerator is set again and the
shortcut is visible. I don't know if it's the best way to reset the
accelerator in setLabel and then setting it back again but at least it
works.
Here's the test file I used:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuBug {
public JMenuBar createMenuBar(final JFrame frame) {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
menuItem = new JMenuItem("menu item");
KeyStroke s = KeyStroke.getKeyStroke(
KeyEvent.VK_T, ActionEvent.META_MASK);
menuItem.setAccelerator(s);
menu.add(menuItem);
return menuBar;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("MenuBug");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MenuBug demo = new MenuBug();
frame.setJMenuBar(demo.createMenuBar(frame));
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Best,
Marco
More information about the macosx-port-dev
mailing list