• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Antoine Koskoz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for replying, but I don't get the point number 4 :/
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks, that was very useful
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On Maneesh's behalf, "you're welcome "


Well, it couldn't have been my post that was useful?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!