Question

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");
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top