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?
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).
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.
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.
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