This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes KeyEvent problem on a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyEvent problem on a JPanel" Watch "KeyEvent problem on a JPanel" New topic
Author

KeyEvent problem on a JPanel

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
OK i have a class that extends JPanel and implements runnable, KeyListener. It has a animation - which is just moving a image. My goal here is to be able to control that animation with arrow keys. Problem is that when i press my keys - nothing happens. I can't find what is the problem. Hope somebody can help me.

[ March 07, 2004: Message edited by: Juhan Voolaid ]
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
This :
while(running=true){
should be
while(running==true){
and this
while(running=false){
should be
while(running==false){

D.
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
thnxs - didn't notice those errors, but the key events still don't happen. I allso have println() functions for keyPressed() method, but still there is something wrong.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Make sure your panel gets focus before pressing any keys... (click on it, *then* press keys).


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Swing is a little picky about using threads.
Here's a way to associate keys with actions and connect them to a JPanel.
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
OK, but if in future my program is going to be a simple game. Should i use timers instead of threads in that case too?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Either way is fine. Threads require a little more care.
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
OK .. i think i do it with threads, but my keyevents still don't work. Does anybody notice what's wrong with my code or know any good tutorial?
Ithink i have problems in focusing this JPanel.
[ March 07, 2004: Message edited by: Juhan Voolaid ]
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
OK finally i solved the problem and in conclusion I say what was wrong.
Problem was that my JPanel lost focus because after this JPanel was initialased some other GUI components were created. So the focus was on last created component.
I got it working when i put super.requestFocus(); to the paintComponent() method in this JPanel class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyEvent problem on a JPanel
 
Similar Threads
createImage() is null
JPanel size
Issue with graphics2d
KeyListener woes
MouseDragged doesn't seem to be working