JDK-4160723 : Menu's accelerator key propagates to textfields
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1998-07-27
  • Updated: 1998-07-28
  • Resolved: 1998-07-28
Related Reports
Duplicate :  
Description

Name: mf23781			Date: 07/27/98


Run the test case Driver below and then follow the instructions:

import com.sun.java.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Driver extends JFrame
{

	JMenuBar	menuBar			= null;
	JMenu		fileMenu		= null;
	JMenuItem	logonMenuItem	= null;
	JMenuItem	exitMenuItem	= null;
	JFrame		frame			= null;

	public static void main(String[] args)
	{
		new Driver();
	}

	public Driver()
	{
		super();
	
		createMenu();
		addEvents();
	
		setBounds(50,50,200,200);
		setVisible(true);

		frame = this;
	}

	public void createMenu()
	{
		menuBar			= new JMenuBar();

		fileMenu		= new JMenu("File");
		fileMenu.setMnemonic((int)'F');
		
		logonMenuItem	= new JMenuItem("Logon");
		logonMenuItem.setMnemonic((int)'L');

		exitMenuItem = new JMenuItem("Exit");
		exitMenuItem.setMnemonic((int)'x');

		fileMenu.add(logonMenuItem);
		fileMenu.addSeparator();
		fileMenu.add(exitMenuItem);
		
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);
	}

	public void addEvents()
	{
		exitMenuItem.addActionListener
		(		
			new ActionListener()
			{
				public void actionPerformed(ActionEvent ae)
				{
					System.exit(0);
				}
			}
		);

		logonMenuItem.addActionListener
		(		
			new ActionListener()
			{
				public void actionPerformed(ActionEvent ae)
				{
					MyDialog md = new MyDialog(frame);
				}
			}
		);
	}

}

class MyDialog extends JDialog
{
	JLabel		label	= null;
	JTextField	text	= null;

	MyDialog(JFrame f)
	{
		super(f);
		getContentPane().setLayout(new FlowLayout());
		label = new JLabel("Label");
		text  = new JTextField(10);
		getContentPane().add(label);
		getContentPane().add(text);
	
		pack();
		setVisible(true);
		text.requestFocus();
	}
}

Press <ALT>+F to expand File menu, press L key to activate 
Logon menuitem.

Our logon dialog box (2 JLabels / 2 JTextfields) appears 
with the letter 'L' in the first text field. 
It appears as if the key event is not being consumed properly.

This occurs on Swing 1.0.2 and Swing 1.0.3
======================================================================

Comments
WORK AROUND Name: mf23781 Date: 07/27/98 If you remove the JTextField.requestFocus() call then no l appears but no components in the new frame receive focus. ======================================================================
11-06-2004