user2535456
user2535456

Reputation: 3

JTabbed Pane is reading keystrokes and switching tabs

I'm using a key listener and a jtabbed pane, and whenever I try to use the left and right keys on the keyboard it just switches between the tabs. This is really annoying since I am actually using the arrow keys in one of the tabs. Is there any way to turn off the "switch tab" keystroke thing in java?

Thank you in advance

KeyListen keylistener = new KeyListen();
    MainGUI.MainTabbedPane.addKeyListener(keylistener);
    MainGUI.MainTabbedPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("LEFT"), "none");
    MainGUI.MainTabbedPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "none");

Upvotes: 0

Views: 175

Answers (1)

nachokk
nachokk

Reputation: 14413

Yes you have to unregister keybinding, you can make something like this

tabComponent.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("LEFT"), "none");
tabComponent.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("RIGHT"), "none");

You may want to take a look at this How to use KeyBindings. It is not recommended to use KeyListeners instead use KeyBindings, cause first you have to have focus and besides keylisteners is for all keys, and keybinding is for specific key.

Upvotes: 1

Related Questions