• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Which is the best way to write action listeners?

 
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.
 
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).
 
Marshal
Posts: 71097
292
  • 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...
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • 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
Marshal
Posts: 71097
292
  • 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
Marshal
Posts: 71097
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic