JMenuItem keyboard accelerator not showing when using the useScreenMenuBar property

Leonid Romanov leonid.romanov at oracle.com
Tue Jun 19 07:40:27 PDT 2012


Yep, thanks! The thing is, this code hasn't changed since 7u4, so I need to find out why it works in 7u4.

On 18.06.2012, at 19:54, Marco Dinacci wrote:

> 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