Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4678019

JMenuItems added directly added JMenuBar cannot be keyboard navigated

      Name: gm110360 Date: 05/01/2002


      FULL PRODUCT VERSION :
      java version "1.4.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b91)
      Java HotSpot(TM) Client VM (build 1.4.0-rc-b91, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      JMenuItems added directly to JMenuBar are keyboard
      inaccessible. Although rare, it is sometimes useful to
      have JMenuItems (called 'bang' menuitems because they
      activate immediately) directly added to a JMenuBar.

      Current problems with adding a JMenuItem directly to a
      JMenuBar:

      1. Unable to navigate to this bang menu item using the
      keyboard's left/right arrow keys (in fact, an exception
      will be thrown).

      2. Bang menu items with a mnemonic will not work; menu
      item with mnemonic assigned does not activate when
      mnemonic pressed.

      3. The JMenuItem's width will extend the remaining width
      of the JMenuBar which is unattractive. To observe this
      select a neighboring JMenu with mouse and hover mouse over
      this bang menu item.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile attached code and run test app.
      2. Select 'File' menu
      3. Press RIGHT cursor key to try to navigate to 'Exit!'
      menu item. Exception will be thrown.
      4. Dismiss menus.
      5. Ensure the 'South' button has focus; press ALT-E
      mnemonic. Notice that the Exit menu item is not activated
      as no message is printed to the console.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.ClassCastException: javax.swing.JMenuItem
              at
      javax.swing.plaf.basic.BasicPopupMenuUI$SelectChildItemAction.actionPerformed
      (BasicPopupM
      enuUI.java:668)
              at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1502)
              at javax.swing.JComponent.processKeyBinding(JComponent.java:2422)
              at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:253)
              at javax.swing.KeyboardManager.fireKeyboardAction
      (KeyboardManager.java:202)
              at javax.swing.JComponent.processKeyBindingsForAllComponents
      (JComponent.java:2499)
              at javax.swing.JComponent.processKeyBindings(JComponent.java:2491)
              at javax.swing.JComponent.processKeyEvent(JComponent.java:2385)
              at java.awt.Component.processEvent(Component.java:4830)
              at java.awt.Container.processEvent(Container.java:1380)
              at java.awt.Component.dispatchEventImpl(Component.java:3526)
              at java.awt.Container.dispatchEventImpl(Container.java:1437)
              at java.awt.Component.dispatchEvent(Component.java:3367)
              at java.awt.KeyboardFocusManager.redispatchEvent
      (KeyboardFocusManager.java:1700)
              at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent
      (DefaultKeyboardFocusManager.java:56
      8)
              at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent
      (DefaultKeyboardFocusManager.java
      :739)
              at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions
      (DefaultKeyboardFocusManager.java
      :673)
              at java.awt.DefaultKeyboardFocusManager.dispatchEvent
      (DefaultKeyboardFocusManager.java:534)
              at java.awt.Component.dispatchEventImpl(Component.java:3396)
              at java.awt.Container.dispatchEventImpl(Container.java:1437)
              at java.awt.Window.dispatchEventImpl(Window.java:1566)
              at java.awt.Component.dispatchEvent(Component.java:3367)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
              at java.awt.EventDispatchThread.pumpOneEventForHierarchy
      (EventDispatchThread.java:190)
              at java.awt.EventDispatchThread.pumpEventsForHierarchy
      (EventDispatchThread.java:144)
              at java.awt.EventDispatchThread.pumpEvents
      (EventDispatchThread.java:138)
              at java.awt.EventDispatchThread.pumpEvents
      (EventDispatchThread.java:130)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.event.*;
      import java.awt.*;
      import javax.swing.*;

      public class MenuTest extends JFrame
      {
          public MenuTest()
          {
              super("Menu Test");
              installMenus();
              installComponents((JPanel) getContentPane());
              addWindowListener(new WindowCloser());
              setBounds(100,100,200,200);
              show();
          }
          
          protected void installMenus()
          {
              JMenuItem item = null;
              
              JMenu fileMenu = new JMenu("File");
              fileMenu.setMnemonic('F');
              fileMenu.add(new JMenuItem("Open")).setMnemonic('O');
              
              JMenu recentMenu = new JMenu("Recent Files");
              recentMenu.setMnemonic('F');
              recentMenu.add(new JMenuItem("One")).setMnemonic('O');
              recentMenu.add(new JMenuItem("Two")).setMnemonic('T');
              fileMenu.add(recentMenu);
              
              JMenuBar menuBar = new JMenuBar();
              menuBar.add(fileMenu);
              item = new JMenuItem("Exit!");
              item.setMnemonic('E');
              item.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      System.out.println("Exit menu item activated");
                  }
              });
              menuBar.add(item);
              
              setJMenuBar(menuBar);
          }
          
          protected void installComponents(JPanel cp)
          {
              popup = new JPopupMenu();
              popup.add(new JMenuItem("One")).setMnemonic('O');
              popup.add(new JMenuItem("Two")).setMnemonic('T');
              
              JMenu subMenu = new JMenu("Off Topic");
              subMenu.setMnemonic('f');
              subMenu.add(new JMenuItem("Apple")).setMnemonic('A');
              subMenu.add(new JMenuItem("Orange")).setMnemonic('O');
              popup.add(subMenu);
              
              cp.addMouseListener(new MouseAdapter() {
                  public void mousePressed(MouseEvent e) {
                      if (e.isPopupTrigger()) {
                          showPopup(e);
                      }
                  }
                  public void mouseReleased(MouseEvent e) {
                      if (e.isPopupTrigger()) {
                          showPopup(e);
                      }
                  }
              });
              JButton button = new JButton("South");
              button.setMnemonic('S');
              button.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      System.out.println("South");
                  }
              });
              cp.add(button, BorderLayout.SOUTH);
          }
          
          protected void showPopup(MouseEvent e)
          {
              popup.show(e.getComponent(), e.getX(), e.getY());
          }
          
          JPopupMenu popup;
          
          public static void main(String[] args)
          {
              new MenuTest();
          }
      }

      ---------- END SOURCE ----------
      (Review ID: 139795)
      ======================================================================

            alexp Alexander Potochkin (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: