JMenuItem keyboard accelerator not showing when using the useScreenMenuBar property

Sergey Bylokhov Sergey.Bylokhov at oracle.com
Thu Aug 9 02:26:09 PDT 2012


Hi Leonid, Marco.
Please file the CR next time, when you realise that this is unknown 
bug(especially if this is regression). So such issues can be tracked and 
not missed.

Thanks.

19.06.2012 18:40, Leonid Romanov wrote:
> 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


-- 
Best regards, Sergey.



More information about the macosx-port-dev mailing list