Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is the best way to write action listeners?

 
Minh Nam
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, there are two ways to handle events, for example:

- using anonymous class action listener, all logic written in method actionPerformed() or by separate methods.
- writing concrete action listener classes (public or inner classes)

So which is the best practice, pros and cons of each, or is there something else?

Please discuss.
Thanks.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, one case where you definitely need to use the concrete class is if you need to have more than one instance of the listener. For instance, if you need two event handlers that work in exactly the same way except that some parameters are different - I'd usually use a named inner class with a constructor that lets me set the parameters and then create as many of them as a necessary.

For simple event handlers, I prefer anonymous inner classes.

(There's a third way, of making the containing class implement the listener interface, but that's usually a poor design).
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was the short answer, from Matthew B. Here is the long answer, and thisis an old discussion about problems with Listeners. The older link at the beginning of the second thread I quoted might be worth reading too.

You will notice MB and I agree completely on this point.
 
Minh Nam
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both guys
I am reading the old posts...
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes Swing's AbstractAction can be put to a really good use, especially if the same action should be accessible from multiple UI controls (eg. a menu item and a toolbar button). It is definitely worth checking out. The greatest thing about it is that you can enable/disable the action itself and all associated controls get updated automatically. Neat.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good idea about Actions. An AbstractAction is an ActionListener, and I tried one for practice here.
 
Minh Nam
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have checked the AbstractAction class and the JMenu and JToolbar, they have a useful method: add(Action) which cuts down the written code significantly.
Thanks guys
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic