JDK-4113639 : Hotkey labels for menus should be left-aligned
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 1998-02-19
  • Updated: 1999-10-13
  • Resolved: 1999-10-13
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Hotkey labels for menus should be left-aligned to give them a professional look.

Steps to reproduce the problem.
1. Run the application for which the sample code is given below.
2. Click on the menu - The hot-key labels Ctrl-C. Ctrl-X, Ctrl-V, Delete are not aligned properly.

-- Sample Code --
import	java.io.*;
import	java.util.*;
import	java.awt.*;
import	java.awt.event.*;
import	com.sun.java.swing.*;

public class TestJMenu17 extends JFrame implements ActionListener{
	/*************************************** Bug Description ***************************************/
	
	String[]	strBugDesc = {	"",
							"",
							"",
							"",
							"This TestCase tests the JMenu Component",
							"",
							"",
							"**Problem : ** Hot-key labels are not left-aligned.",
							"",
							"",
							"Steps to reproduce :",
							"1. Click on the menu, and the menu items drop down.",
							"2. The hotkey labels - Ctrl-X, Ctrl-C, Ctrl-V and Delete are not left aligned.", 
							"3. Can also check for different L&N by selecting from the toolbar.",
							"",
							"",
							"",
							""};
 	/*************************************** Bug Description ***************************************/


	Container		content;
	JToolBar		toolbar;
	JMenuBar		mbar;

	static String		strMotif = "Motif";
	static String 		motifClassName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
		
	static String		strWindows = "Windows";
	static String		windowsClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

	static String		strMetal = "Metal";
	static String		metalClassName = "com.sun.java.swing.plaf.metal.MetalLookAndFeel";

	static		String[]	strData = {"Cut", "Copy", "Paste", "Delete"};
	static		char[]		mnData = {'C', 'o', 'P', 'D'};

	
	TestJMenu17(String[]	args) {
		displayBugDesc();
		content = getContentPane();
		content.setLayout(new BorderLayout(8, 8));
		addWindowListener(new WindowAdapter() {
									public void windowClosing(WindowEvent event){
										System.exit(0);
									}
								}
		);

		toolbar = CreateToolBar();
		content.add("North", toolbar);

		content.add("Center", getMyMenuBar());

		pack();
		show();
	}


	public void displayBugDesc() {
		int n = strBugDesc.length;
		System.out.println("/******************************* Bug Description  and Comments *******************************/");
		System.out.println();
		for(int i = 0; i < n; i ++) {
			System.out.println(strBugDesc[i]);
		}
		System.out.println();
		System.out.println("/******************************* Bug Description  and Comments *******************************/");
	}


	public JMenuBar	getMyMenuBar() {
		JMenuBar			menubar;
		JMenu					menu;
		JMenuItem			menuitem;
		int i = 0;
		menubar = new JMenuBar();
		menu = new JMenu("Edit");
		menu.setMnemonic('E');
	
		
		menuitem = new JMenuItem(strData[i]);
		menuitem.setMnemonic(mnData[i]);
		menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		menuitem.addActionListener(this);
		menu.add(menuitem);
		i ++;
	

		menuitem = new JMenuItem(strData[i]);
		menuitem.setMnemonic(mnData[i]);
		menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		menuitem.addActionListener(this);
		menu.add(menuitem);
		i ++;

		menuitem = new JMenuItem(strData[i]);
		menuitem.setMnemonic(mnData[i]);
		menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
		menuitem.addActionListener(this);
		menu.add(menuitem);
		i ++;

		menuitem = new JMenuItem(strData[i]);
		menuitem.setMnemonic(mnData[i]);
		menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.CHAR_UNDEFINED));
		menuitem.addActionListener(this);
		menu.add(menuitem);


		menubar.add(menu);
		return menubar;
	}


	public JToolBar	CreateToolBar() {
		JToolBar tbar = new JToolBar();
		JButton	bn;

		bn = new JButton(strWindows);
		bn.addActionListener(this);
		bn.setActionCommand(windowsClassName);
		tbar.add(bn);

		bn = new JButton(strMotif);
		bn.addActionListener(this);
		bn.setActionCommand(motifClassName);
		tbar.add(bn);

		bn = new JButton(strMetal);
		bn.addActionListener(this);
		bn.setActionCommand(metalClassName);
		tbar.add(bn);

		return tbar;
	}


	public void actionPerformed(ActionEvent	e) {
		String		str = e.getActionCommand();
		if(str.equals(strData[0]) || str.equals(strData[1]) || str.equals(strData[2]) || str.equals(strData[3])) {
			System.out.println("Action Event : " + e.paramString());
		}

		else {
			try {
				UIManager.setLookAndFeel(str);
				SwingUtilities.updateComponentTreeUI(TestJMenu17.this);
				TestJMenu17.this.pack();
			} 
			catch (Exception exc) {
				System.err.println("Could not load LookAndFeel: " + str);
			}
		}
	}

	


	// Main funtion	
	public static void main(String[]	args) {
		TestJMenu17	test = new TestJMenu17(args);
	}
}

-- Sample Code --

Comments
EVALUATION This is not as simple as it might appear, since you have to know the length/size of the text of all sibling menus in order to determine the correct position for the accelerator text to begin.
11-06-2004