3

I'm working on a GUI.

The Gui is constructed as follows: A JFrame containining a (custom) leftPanel and rightPanel. RightPanel is an extension of a tabbedpane and contains as one of the tabs, the class MainTab.

Maintab has the following code:

private void createKeyBindings(){
    actionMap = this.getActionMap();
    inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(KeyStroke.getKeyStroke("UP),
            "pressedUpArrow");
    inputMap.put(KeyStroke.getKeyStroke("released UP"),
           "releasedUpArrow");
    actionMap.put("pressedUpArrow",
             new PressedUpArrowAction());
    actionMap.put("releasedUpArrow",
            new ReleasedUpArrowAction());
    inputMap.put(KeyStroke.getKeyStroke("RIGHT"),
            "pressedRightArrow");
    inputMap.put(KeyStroke.getKeyStroke("released RIGHT"),
           "releasedRightArrow");
    actionMap.put("pressedRightArrow",
             new PressedRightArrowAction());
    actionMap.put("releasedRightArrow",
            new ReleasedRightArrowAction());
    inputMap.put(KeyStroke.getKeyStroke("LEFT"),
            "pressedLeftArrow");
    inputMap.put(KeyStroke.getKeyStroke("released LEFT"),
           "releasedLeftArrow");
    actionMap.put("pressedLeftArrow",
             new PressedLeftArrowAction());
    actionMap.put("releasedLeftArrow",
            new ReleasedLeftArrowAction());
    inputMap.put(KeyStroke.getKeyStroke("RIGHT"),
            "pressedRightArrow");
    inputMap.put(KeyStroke.getKeyStroke("released RIGHT"),
           "releasedRightArrow");
    actionMap.put("pressedRightArrow",
             new PressedRightArrowAction());
    actionMap.put("releasedRightArrow",
            new ReleasedRightArrowAction());
    inputMap.put(KeyStroke.getKeyStroke("DOWN"),
            "pressedDownArrow");
    inputMap.put(KeyStroke.getKeyStroke("released DOWN"),
           "releasedDownArrow");
    actionMap.put("pressedDownArrow",
             new PressedDownArrowAction());
    actionMap.put("releasedDownArrow",
            new ReleasedDownArrowAction());
}

So, it has to undertake an action when I press one of the arrow keys, and when I release it.

But it only does something when I release one of the arrow keys. The cause is probably that one of the other components of LeftPanel or RightPanel or the panels itself already use that keybinding for something else.

I've tried to just clear the inputmaps and actionmaps in the classes themselves ( this.getInputMap().clear() ), but the problem remains. Any solutions?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sven
  • 1,133
  • 1
  • 11
  • 22
  • 1
    sorry I can't see any concurency betweens `JComponents` in the current `JPanel` and `tabs` from `JTabbedPane` and `KeyBindings`, for better help sooner post an [SSCCE](http://sscce.org/), otherwise you can to play with `JComponent.WHEN_IN_FOCUSED_WINDOW` – mKorbel Oct 29 '12 at 14:59
  • 3
    Here try this wonderful [example](http://stackoverflow.com/a/8961998/1057230) by @HovercraftFullOfEels. – nIcE cOw Oct 29 '12 at 15:59

1 Answers1

0

When you register arrow key strokes but the corresponding action is not executed (while actions associated with other key strokes, like letters, work perfectly fine), the reason might be that a JButton or a JTextField somewhere on your component is intercepting these arrow key strokes.

To fix this, call yourJButton.setFocusable(false) and yourJTextfield.setFocusable(false). That will stop the JButton and the JTextField from consuming the arrow key strokes.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171