• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

KeyEvent problem on a JPanel

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure your panel gets focus before pressing any keys... (click on it, *then* press keys).
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either way is fine. Threads require a little more care.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic