-
Bug
-
Resolution: Duplicate
-
P3
-
None
-
1.3.0
-
x86
-
windows_nt
Name: rlT66838 Date: 04/10/2000
C:\jdk1.3\bin>java -version
java version "1.3.0rc2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc2-Y)
Java HotSpot(TM) Client VM (build 1.3.0rc2-Y, mixed mode)
// This program illustrates two problems with the way that menu mnemonics
// are processed in a JFrame.
// Problem #1 occurs when a menu uses a mnemonic that also appears anywhere
// on the menuBar. The mnemonic keystroke triggers the event associated with
// the item on the menu, not the item on the menuBar. It shows up in many
// different ways. For example:
// A. Start up the enclosed application, then press Alt-N. About 1/2 of the
// time it will display Menu2 and the other 1/2 of the time it will fire
// the event for Menu2/Option2 (when menu2/option2 fires it will print a
// message to the console).
// B. Clear any menus. Hold down the Alt key, then press M, N. The first
// key press displays Menu 1, as it should. The second keystroke displays
// menu2 instead of option 2 on menu 1.
// c. If you press Alt-M, release the Alt key and press N, it fires the
// event for Option 2 on Menu 1. This behavior, however, is
// contrary to standard Windows behavior.
// D. Press Alt-N, Alt-N. This should fire the event for Option 2 on menu 2.
// It does not.
// The common issue is that items on the menuBar do not act independently of
// the items on the pulldown menus. A mnemonic of N on the menuBar gets
// confused with the mnemonic of N on the pulldown menu.
// Problem #2 occurs when a mnemonic is assigned to a sub-menu.
// It does not work. See Menu2/Sub-Menu 1
package ssi.javabugs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MnemonicBug2 extends JFrame
{
public MnemonicBug2()
{
// Create menu bar
JMenuBar menuBar = new JMenuBar(); // Create menu bar
setJMenuBar(menuBar); // Add menu bar to window
// Create first option on menuBar
JMenu m1 = new JMenu("Menu1");
m1.setMnemonic('M');
JMenuItem m1o1 = new JMenuItem("Option1");
m1o1.setMnemonic('O');
m1o1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu1/Option1 was selected");
}
}
);
m1.add(m1o1);
JMenuItem m1o2 = new JMenuItem("Option2");
m1o2.setMnemonic('N');
m1o2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu1/Option2 was selected");
}
}
);
m1.add(m1o2);
// Create second option on menuBar
JMenu m2 = new JMenu("Menu2");
m2.setMnemonic('N');
JMenuItem m2o1 = new JMenuItem("Option1");
m2o1.setMnemonic('O');
m2o1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu2/Option1 was selected");
}
}
);
m2.add(m2o1);
JMenuItem m2o2 = new JMenuItem("Option2");
m2o2.setMnemonic('N');
m2o2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu2/Option2 was selected");
}
}
);
m2.add(m2o2);
// Create Sub-Menu
JMenu sm1 = new JMenu("Sub-menu");
sm1.setMnemonic('S');
JMenuItem smo1 = new JMenuItem("Sub menu option 1");
smo1.setMnemonic('O');
smo1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Sub-Menu/Option 1 was selected");
}
}
);
sm1.add(smo1);
JMenuItem smo2 = new JMenuItem("Sub menu option 2");
smo2.setMnemonic('N');
smo2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Sub-Menu/Option 2 was selected");
}
}
);
sm1.add(smo2);
m2.add(sm1);
menuBar.add(m1);
menuBar.add(m2);
this.setTitle("Mnemonic Bugs");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
setSize(400,250);
show();
}
public static void main(String[] args)
{
MnemonicBug2 mnemonicBug = new MnemonicBug2();
}
}
(Review ID: 103494)
======================================================================
- duplicates
-
JDK-4213634 Alt+menmonic char not working when menu & menuitem have same mnemonic char.
-
- Closed
-