7

I have a menu item, "rename", for which F2 is set as an accelerator. Indeed when the menu is displayed there a little "F2" indication next to "rename".

Sadly, this does not work. This accelerator triggers no response. When I change the accelerator to CTRL+F2 - it works.

It seems that I should use an InpoutMpa/ActionMap. The problem with that is that I want this to work everywhere in the app so I am trying to associate it with the top-level JFrame. But, JFrame does not have a getInputMap() method.

Lost.

[Added]

     ks = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);
     JMenuItem mi = new JMenuItem("Rename");
     mi.setAccelerator(ks);
     mi.addActionListener(action); 
2
  • 1
    Please show us the coding you use to set the accelerator! Jan 7, 2010 at 10:02
  • 3
    Could it be that your OS/window manager "consumes" the keystroke so it actually never arrives at your Swing application (e.g. because F2 is mapped to something special, and Ctrl-F2 is not)? Jan 7, 2010 at 15:47

2 Answers 2

5

This is probably happening because JTable uses F2 to invoke the StartEditing action (I saw the same behavior on one of my programs and traced it to this).

There are a couple of solutions. The most drastic is to remove this input mapping (I believe this code actually removes the mapping from all JTables):

KeyStroke keyToRemove = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);

InputMap imap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
while (imap != null)
{
    imap.remove(keyToRemove);
    imap = imap.getParent();
}

Or, if you're using the table for display only, and don't plan to let the user edit it, you could make it non-focusable:

table.setFocusable(false);

On a completely different subject, I strongly recommend creating an AbstractAction subclass for your menu items, rather than creating them "from scratch". Aside from giving you very simple menu setup code, you can use the same action instance for both the main menu and a popup/toolbar, and enable/disable them both at the same time.

5

I know this is an old thread, but I struggled with the exact same thing as the original poster and found the solution. The JFrame itself doesn't have a getInputMap method, but its root pane does. So you have to use "getRootPane.getInputMap" instead.

Example code:

public class ApplicationFrame extends JFrame {
    private AbstractAction f2Action = new AbstractAction() {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            // Do something useful
        }
    };

    public ApplicationFrame() {

        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getRootPane().getActionMap(); 

        inputMap.put(KeyStroke.getKeyStroke("F2"), "f2Action");
        actionMap.put("f2Action", f2Action);

    }
}
2
  • 1
    hmm .. don't think that this binding would work when a menu accelerator doesn't.
    – kleopatra
    Oct 26, 2012 at 10:20
  • The accelerator is a binding that is added somewhere. The problem is that something along the component tree is capturing the keystroke before it reaches the binding. I solved this by writing a method that traverses an entire Container tree and removes the keystroke from the input map of any JComponent it finds. Nov 22, 2013 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.