> I'm having problems tying the F1 key to the various menuitems in a
> JMenu object to provide context sensitive help.
>
> When the menu is open, and the F1 key is pressed, it seems that the
> ActionMap for the last menuitem in the menu always gets activated,
> even when no menuitem isn't selected yet.
>
> Any help with this would be greatly appreciated.
I am unable to reproduce this effect on my implementation. When the menu
is open, both a mnemonic and an accelerator key work as expected. In
looking at the tutorials, "How to Use Menus" and "How to Use Actions,"
<http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html>
<http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html>
I don't see anything that requires manipulating an ActionMap directly.
Can you post a short example that shows the problem you describe?
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
> On Mon, 02 Nov 2009 11:06:32 -0500, "John B. Matthews"
> <nos...@nospam.invalid> wrote:
>
> > Can you post a short example that shows the problem you describe?
>
> I have no problems using mnemonics or action objects with menuitems.
> My objective is to provide context sensitive help on each menuitem in
> a menu, by tying the F1 key to each of the menuitems, using
> getInputMap() and getActionMap(), so when F1 is pressed the
> appropriate action is executed, (which will open a help window on the
> menuitem).
>
> Here's how I tie F1 to the menuitem:
>
> KeyStroke keyF1 = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
> menuItem.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(keyF1, "help");
> menuItem.getActionMap().put("help", menuHelp);
>
> Here's the action that is to be executed when F1 is pressed :
>
> Action menuHelp = new AbstractAction() {
> public void actionPerformed(ActionEvent evt) {
> ....
> }
> };
>
> I have various menuitems in a JMenu object, but when the menu is
> opened and when F1 is pressed, only the action which is tied to the
> last menuitem in the menu is executed, even though none or some other
> menuitem is selected.
Ah, you're doing something along theses lines:
<http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html>
IIUC, key bindings are used behind the scenes by mnemonics and
accelerators. Does that conflict with your usage? I presume you're
avoiding duplicate WHEN_IN_FOCUSED_WINDOW bindings. Sorry I can't be
more help.