-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.1.2, 1.2.0
-
None
-
x86, sparc
-
solaris_2.6, windows_nt
The action command generated when a menu shortcut is entered is always null unless setActionCommand() is used. THis is because MenuItem.handleShortcut() uses actionCommand instead of getActionCommand() when creating the ActionEvent. The fix is to use getActionCommand() instead. THis way, the event generated is consistent with that generated by mouse actions over the same menu item.
==========================================================
Another report of this problem with an example:
A MenuShortcut added to a MenuItem fires the corresponding
ActionEvent (on actionPerformed) but the call to getActionCommand() on the ActionEvent received returns null, resulting on the user being unable to identify the command launched.
import java.awt.*;
import java.awt.event.*;
public
class KeyTest
extends Frame
implements ActionListener
{
public
KeyTest()
{
super("test");
MenuBar mb = new MenuBar();
Menu t = new Menu("test");
MenuItem mt = new MenuItem("test 1", new MenuShortcut('s'));
mt.addActionListener(this);
t.add(mt);
mt = new MenuItem("test 2", new MenuShortcut('x'));
t.add(mt);
mt.addActionListener(this);
mb.add(t);
setMenuBar(mb);
}
public
void
actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
}
public static
void
main(String[] s)
{
KeyTest t = new KeyTest();
t.setSize(200,200);
t.show();
}
}
==========================================================
Another report of this problem with an example:
A MenuShortcut added to a MenuItem fires the corresponding
ActionEvent (on actionPerformed) but the call to getActionCommand() on the ActionEvent received returns null, resulting on the user being unable to identify the command launched.
import java.awt.*;
import java.awt.event.*;
public
class KeyTest
extends Frame
implements ActionListener
{
public
KeyTest()
{
super("test");
MenuBar mb = new MenuBar();
Menu t = new Menu("test");
MenuItem mt = new MenuItem("test 1", new MenuShortcut('s'));
mt.addActionListener(this);
t.add(mt);
mt = new MenuItem("test 2", new MenuShortcut('x'));
t.add(mt);
mt.addActionListener(this);
mb.add(t);
setMenuBar(mb);
}
public
void
actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
}
public static
void
main(String[] s)
{
KeyTest t = new KeyTest();
t.setSize(200,200);
t.show();
}
}
- duplicates
-
JDK-4079449 MenuItem objects return null if they are activated by shortcut
-
- Resolved
-