• 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

KeyListener and ActionListener error.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!


 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic