• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyListerner in JPanel

 
Arthur Sc Chan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code abstract. Can anyone help me to solve this problem?? Thanks.

 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic