JDK-4770012 : Menu and Button Mnemonic Underlining Breaks in Windows L&F
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2002-10-28
  • Updated: 2002-10-30
  • Resolved: 2002-10-30
Related Reports
Duplicate :  
Description

Name: jk109818			Date: 10/28/2002


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


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


A DESCRIPTION OF THE PROBLEM :

This problem is discussed in the following forum:
http://forum.java.sun.com/thread.jsp?forum=31&thread=316561

This problem is with Windows L&F.  The same application works 
perfectly under RH Linux 7.3.

My problem is not related to having a menuItem with the same mnemonic 
as a menu.  I seem to have identified the problem as being connected 
with adding a button (or some other component) that also has a mnemonic.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile & run the code!
2. Press ALT key to see menu mnemonics.  Underlines appear.
3. Press the search button
4. Now press the ALT button again, and underlines no longer appear.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Pressing the Search Button should not break mnemonic underlining.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No error messages are generated by the problem.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;

import javax.swing.JButton;
import javax.swing.UIManager;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;

/*
 * This class exists solely to show you what menus look like.
 * It has no menu-related event handling.
 */
public class MenuLookDemo extends JFrame {
    JTextArea output;
    JScrollPane scrollPane;

  public MenuLookDemo() {
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;
    JCheckBoxMenuItem cbMenuItem;
    JRadioButtonMenuItem rbMenuItem;

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    //Add regular components to the window, using the default BorderLayout.
    Container contentPane = getContentPane();
    output = new JTextArea(5, 30);
    output.setEditable(false);
    scrollPane = new JScrollPane(output);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    //Create the menu bar.
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    //Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription(
            "The only menu in this program that has menu items");
    menuBar.add(menu);

    //a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item",
                             KeyEvent.VK_T);
    //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription(
            "This doesn't really do anything");
    menu.add(menuItem);

    menuItem = new JMenuItem("Both text and icon",
                             new ImageIcon(getClass().getResource
("images/middle.gif")));
    menuItem.setMnemonic(KeyEvent.VK_B);
    menu.add(menuItem);

    menuItem = new JMenuItem(new ImageIcon(getClass().getResource
("images/middle.gif")));
    menuItem.setMnemonic(KeyEvent.VK_D);
    menu.add(menuItem);

    //a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    menu.add(rbMenuItem);

    //a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    menu.add(cbMenuItem);

    //a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_2, ActionEvent.ALT_MASK));
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    submenu.add(menuItem);
    menu.add(submenu);

    //Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription(
            "This menu does nothing");
    menuBar.add(menu);

/* Adding the button causes the problem when using Windows LnF
*/
    JButton jButton_Search = new JButton();
    jButton_Search.setMnemonic(KeyEvent.VK_S);
    jButton_Search.setText("Search");
    getContentPane().add(jButton_Search, BorderLayout.SOUTH);

  }

  public static void main(String[] args) {

/* Commenting out the UIManager stuff below will make the program "work" with
Windows
*/
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
      e.printStackTrace();
    }

      MenuLookDemo window = new MenuLookDemo();

      window.setTitle("MenuLookDemo");
      window.setSize(450, 260);
      window.setVisible(true);
  }
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Don't use the Window LnF?

Except that I want to... http://thomasfly.com/FBN
(Review ID: 166312) 
======================================================================

Comments
EVALUATION Name: azR10139 Date: 10/30/2002 This bug is a duplicate of bug 4736093. Suggested fix for bug 4736093 fixes this bug. ======================================================================
24-08-2004