File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyEvent problem on a JPanel

 
Juhan Voolaid
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This :
while(running=true){
should be
while(running==true){
and this
while(running=false){
should be
while(running==false){

D.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure your panel gets focus before pressing any keys... (click on it, *then* press keys).
 
Craig Wood
Ranch Hand
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either way is fine. Threads require a little more care.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic