=======================================================================================================
// ActionTest.java -- This program tests whether an Action object that extends
// AbstractAction can place a mnemonic on a JMenuItem and a JButton
//
// Problem = On Uw7.1.1 with JRE-1.3.1_06, and JLS xmodmap changes to
// enable ALT, (see: http://tinyurl.com/5iar ) it appears that on the
// JButton:
//
// * the mnemonic doesn't underline the label letter.
// * the mnemonic induced actionPerformed doesn't work.
// * the mnemonic isn't listed in the tooltip.
// * the tooltip otherwise works ok.
// * clicking the JButton causes the actionPerformed ok.
// * All other components work ok.
//
// This is the simplest example I could reduce from the
// Java Tutorial, "How To Use Actions", http://tinyurl.com/5ib4
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.AbstractAction;
public class ActionTest extends JFrame {
private Action colorAction; // Does this action work correctly?
private JPanel mainPanel; // Holds a JButton
private Color bgColor[] = {Color.black, Color.yellow}; // The actionPerformed changes colors.
private int index = 0; // keeps track of the bgColor array.
public ActionTest() {
super("Testing Action object mnemonics on a JButton");
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
// create the Action. ColorAction extends AbstractAction in an inner class below....
colorAction = new ColorAction( "Change Color", // name
"Change the background color.", // tooltip
new Integer(KeyEvent.VK_C)); // mnemonic
// create Menus
JMenuBar bar = new JMenuBar(); // create the JMenuBar
setJMenuBar(bar); // attach JMenuBar to JFrame
JMenu menu = new JMenu("File"); // create a JMenu
menu.setMnemonic(KeyEvent.VK_F); // set it's Mnemonic
bar.add(menu); // add it to the JMenuBar
JMenuItem menuItem = new JMenuItem( colorAction ); // create a JMenuItem
menu.add(menuItem); // add it to the File menu.
// create a JButton in a JPanel w/the same Action and add it to the ContentPane
JButton bgButton = new JButton( colorAction );
mainPanel = new JPanel( new FlowLayout(FlowLayout.CENTER) );
mainPanel.add(bgButton);
getContentPane().add(mainPanel);
setSize(400,100);
setLocation(200,200);
setVisible(true);
} // end of ActionTest Constructor
public class ColorAction extends AbstractAction {
public ColorAction(String name, String tip, Integer mnemonic) {
super(name);
putValue(SHORT_DESCRIPTION, tip);
putValue(MNEMONIC_KEY, mnemonic); // Mnemonic defined here.
}
public void actionPerformed(ActionEvent e) {
if (index > 1 ) index = 0;
mainPanel.setBackground(bgColor[index++]); // The action paints the background.
}
} // end of ColorAction inner class
public static void main(String arg[]) {
ActionTest app = new ActionTest();
} // end of Main
}// end of ActionTest Class
// The mnemonic to bring down the File menu, Alt+F works, as does
// the colorAction mnemonic C when the File menu is visible. So we've
// shown that the actionPerformed works, just not the mnemonic on the
// JButton.
//
// Thanks for looking at this.
// Matt Schalit
============================================================================================================
For the record, this is a known problem in Sun's (and thus SCO's)
J2SE 1.3.x, but is fixed in Sun's J2SE 1.4.
The work-around is to assign the MNEMONIC_KEY to the action AFTER
the action is passed into the constructor of the JButton.
Jonathan Schilling