• 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

ActionListeners & Classes

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I have a question about ActionListeners, my application class entends from JFrame and implements ActionListener so it has a actionPerformed method within that class.

So can i get access to that actionPerformed method in a different class? and if so how?

Kind Regards

Tim
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From another class, you would call a component's addActionListener() method and pass in an instance of your JFrame/ActionListener class. For example:



So whenever 'button' is clicked, the actionPerformed() method from your JFrame/ActionListener class will execute.
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it more light weight to implements the ActionListener in other separate class instead of in JFrame? Where later on it can be use by the JFrame and other classes that reuse this ActionListener.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I Wayan Saryada wrote:Isn't it more light weight to implements the ActionListener in other separate class instead of in JFrame? Where later on it can be use by the JFrame and other classes that reuse this ActionListener.



That's generally the way to go. Who knows, though, maybe the OP is doing what he's doing for a specific reason.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I shall move this discussion to our GUIs forum.

There is no difference in weight between having Listeners in different sorts of classes. No design is heavyweight or lightweight. There are object‑oriented and non‑object‑oriented versions however. I have a little rule of thumb about Listeners, and where you put their classes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic