This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes KeyListerner in JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListerner in JPanel" Watch "KeyListerner in JPanel" New topic
Author

KeyListerner in JPanel

Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
My program has one JFrame and inside the JFrame there are three JPanel and I add a keylistner to one of the JPanel. After that I overwrite the keyPressed method and try to do something when the arrow key is pressed. But I found that nothing happen when I press the arrow key. Also, I have checked that the keyPressed method has not been called. I have already set the requestfocus to this JPanel but still nothing happen when I press the arrow key. Can anyone help me in this problem?? Thanks a lot.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

From what you described it should work. The arrow keys trigger "virtual key" events, which are handled by the keyPressed() method, so you've overridden the correct one.
One thing that comes to mind is that you may have invoked requestFocus() / requestFocusInWindow() before you display the JFrame, using setVisible(true).
You could check that. Beyond that we'd really need to see some code.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
KeyEvents can only be dispatched to the component with focus. By default panels don't receive focus.

The better approach is to use Key Bindings.
Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
Jelle Klap wrote:
One thing that comes to mind is that you may have invoked requestFocus() / requestFocusInWindow() before you display the JFrame, using setVisible(true).


I am sorry that I am new to Java GUI and I want to know that how to check that if I have invoked requestFocus before displaying the JFrame??
Actually, I add the key listener directly in the JPanel. I create a private inner KeyListener class in that JPanel and add the KeyListener to the JPanel. I do the same thing using MouseMotionListener and it goes all well. But it is not the case for KeyListener which still nothing happen and keyPressed method are still not been called.
Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
Rob Camick wrote:

The better approach is to use Key Bindings.


I have read that link from oracle before and I does not have a great understanding in Key Binding. Can you just give me a simple example on the usage of Key Binding?? Also, what is the major difference between Key Listener and Key Binding??
Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
Here is my code abstract. Can anyone help me to solve this problem?? Thanks.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
That extract doesn't help. You need to post a SSCCE.

The suggestion was to use Key Bindings. Where is the code that shows you tried to use Key Bindings based on the examples from the Swing tutorial?.

You get one free example that shows simple usage of Key Bindings for the left/right keys. Next time post your example code which shows that you at least made an effort to read and understand the tutorial!

 
Don't get me started about those stupid light bulbs.
 
subject: KeyListerner in JPanel
 
Similar Threads
Listening for keyPressed Event
Game(ball breaker) developed using swings not running.
I'm having trouble with my game...
Can't get arrow keys for JTable if on a JTabPane
KeyListener