• Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyListener and ActionListener error.  RSS feed

 
Manuela Sucerquia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a problem with a code I had to work on. I have no idea why the KeyListener or de ActionListener is not working. What could it be? Note: I need to stop, restart, speed up and slow down the movement on the "pendulum", that's why the keys are used for.
Thanks!


 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuela Sucerquia wrote:Hi, I have a problem with a code I had to work on. I have no idea why the KeyListener or de ActionListener is not working. What could it be? Note: I need to stop, restart, speed up and slow down the movement on the "pendulum", that's why the keys are used for.
Thanks!

Java is kind of strange in that an Object can implement an ActionListener or KeyListener, but until you actually set the KeyListener or ActionListener for an Object it is never registered to receive Actions. So you have register the Listeners with the Objects you want to have Listen before they will use your implementation of ActionLister and KeyListener to process Actions.

I don't see where you add the KeyListener or ActionListener to anything, so...
 
Manuela Sucerquia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
Manuela Sucerquia wrote:Hi, I have a problem with a code I had to work on. I have no idea why the KeyListener or de ActionListener is not working. What could it be? Note: I need to stop, restart, speed up and slow down the movement on the "pendulum", that's why the keys are used for.
Thanks!

Java is kind of strange in that an Object can implement an ActionListener or KeyListener, but until you actually set the KeyListener or ActionListener for an Object it is never registered to receive Actions. So you have register the Listeners with the Objects you want to have Listen before they will use your implementation of ActionLister and KeyListener to process Actions.

I don't see where you add the KeyListener or ActionListener to anything, so...


Hey, thanks for your answer!
Actually, I have no idea what I'm doing. I just had one example on how to do the keylistener thing, and it's not working (obviously). How should I declare them?
*I'm sorry, I'm new in this. I'm trying my best, I swear :c
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add a javax.swing.Timer object and set it up for the refresh rate you want and it will use your ActionListener, once more as a "this" entry. You should probably look at this previous link on doing basically just want you are trying to do now. There is a good discussion there and examples.

Manuela Sucerquia wrote: I just had one example on how to do the keylistener thing, and it's not working (obviously). How should I declare them?
*I'm sorry, I'm new in this. I'm trying my best, I swear :c


In your main, change to following:

 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have too much going on in your paint--and it should be in a paintComponent instead--you can do all of this calculation outside your paintComponent--render to a BufferedImage and then in your paintComponent all you need do is:

Manuela Sucerquia wrote:Hi, I have a problem with a code I had to work on. I have no idea why the KeyListener or de ActionListener is not working. What could it be? Note: I need to stop, restart, speed up and slow down the movement on the "pendulum", that's why the keys are used for.
Thanks!

 
Fred Kleinschmidt
Bartender
Posts: 555
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of the GUI code should be executed on the Event thread, not the main thread. the main() module should lokk something like this:



then you create your app code. The JFrame is included as a constructor argument for convenience, so you can add any key listeners (or other things) to it if desired
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!