Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanel keylistener

Tags:

java

swing

jpanel

I am trying to add a key listener that holds a JTabbedPane.
It should switch the tabs when ctrl + tab is received.
But the keypressed event is never sent I tried adding it to the panel and to the tabbed object - but with no success.

Here is my code

SwitchTabsListener ctrlTabListener = new SwitchTabsListener(genericTabbedPanel);  
jMainFrame.addKeyListener(ctrlTabListener);  
genericTabbedPanel.addKeyListener(ctrlTabListener);  
like image 332
Bick Avatar asked Jan 24 '11 10:01

Bick


People also ask

Can you add a KeyListener to a JPanel?

addKeyListener() doesn't work for JPanel.

What is KeyListener in Java?

Interface KeyListenerThe listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).

What does keyPressed do in Java?

In Java, there are three types of KeyEvent. The types correspond to pressing a key, releasing a key, and typing a character. The keyPressed method is called when the user presses a key, the keyReleased method is called when the user releases a key, and the keyTyped method is called when the user types a character.


2 Answers

In a typical fashion, your key event is not intercepted by the correct Swing component. You have to understand that the first component below the cursor will receive the keyboard event. Were you to select a button with your keyboard, it would be this JButton that would receive the key event.

To make sure you get all those events, you don't have to register on components, but rather by using a KeyboardFocusManager, which will receive key events wherever they occur.

Your code then require the following elements

KeyEventDispatcher myKeyEventDispatcher = new DefaultFocusManager();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(myKeyEventDispatcher);

myKeyEventDispatcher will then receive calls to dispatchKeyEvent whenever a key is pressed, wherever it is in UI. This way, you can make sure your code is correctly called.

The alternative method of registering key listener would require you to use a HierarchyListener in order for your key listener to be added:removed to each and every swing component that appear to be added/removed as a child of your root component. This is not only cumbersome to write, but also really hard to debug and understand.

This is why I prefer the more brute-force, but although quite elegant way of adding application global key listener to a specific keyboard focus manager.

like image 61
Riduidel Avatar answered Oct 28 '22 14:10

Riduidel


Ctrl+Tab and Ctrl+Shift+Tab allow you to cycle through tabs by default in the Windows LookAndFeel:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
like image 22
dogbane Avatar answered Oct 28 '22 14:10

dogbane