• 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

Using one "actionPerformed" by class or one by component ?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I'd like to know which is the best option between using once in a class "actionPerformed" or as many time as I have components which needs it like that :




Thanks.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess would be as many as you need base on what you want from
your component.

Take a look at:

http://download.oracle.com/javase/tutorial/uiswing/events/intro.html


G.

 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, why aren't my links working?

http://download.oracle.com/javase/tutorial/uiswing/events/intro.html

 
Antoine Koskoz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in the link they use the method outside a component, directly in the class and not as the code I wrote above.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends.

Usually there are four ways you can do it.
1) Have the class implement action listener. This is not really a good idea. In complex UIs you typically have multiple action sources. In the action performed you end up with multiple statements checking for the action source.
2) Have anonymous an inner class like your code.
3) Have the component itself implement the action listener. Might seem modular but it becomes tightly coupled. But then you need to subclass the component. Also doesn't handle the situation in #4 below
4) Subclass abstract action and share it between action sources. e.g. Your UI might have a menu like File->Exit as well as a button on the toolbar which allows the user to exit the application.

Rule of the thumb. When you have a single action source with a unique action, use anonymous inner class. When you got multiple sources with the same action, go for #4
 
Antoine Koskoz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying, but I don't get the point number 4 :/
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote: . . . Have the class implement action listener. This is not really a good idea. . . .

Not really a good idea? That is a bit like saying "Bill Gates has enough money to buy me dinner." It's a dreadful idea to have the GIU implement ActionListener.

Never mind about this being a 5-year old posting, the ideas in it haven't changed at all. And look at the instructions how to create an anonymous ActionListener, and when to use anonymous classes or named classes. Only one change I would suggest: change public void actionPerformed( . . . ) to @Override public void actionPerformed( . . . ). the @Override annotation allows you to use Adapter classes (eg MouseAdapter) and to be sure you haven't got some subtle error with the spelling of the method name.
And, yes, I am linking to my own posts. Why should I repeat what I said earlier?
 
Antoine Koskoz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that was very useful
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On Maneesh's behalf, "you're welcome "


Well, it couldn't have been my post that was useful?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Not really a good idea? That is a bit like saying "Bill Gates has enough money to buy me dinner." It's a dreadful idea to have the GIU implement ActionListener.



Something compels me to point out that in Java 1.0, subclassing a Component was the only way to handle events, and the ability to not do it that way was the most touted feature introduced in the first major revision of Java (1.1). So people have known this was a bad idea practically from day one!
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't know that . . . at least that bad idea could be forgotten about. Not like missing out generics at the outset, which means we have awkward generics now. Or the java.util.Stack class.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic