drg
drg

Reputation: 69

When to use Action over ActionListener

The Java API says,

"The Action interface provides a useful extension to the ActionListener interface in cases where the same functionality may be accessed by several controls."

I can do the same with custom inner class listeners. What am I missing?
I do see some of the benefits of Action (icons, descriptions, enabled state).

Upvotes: 0

Views: 138

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347234

Generally speaking, I prefer Action over ActionListener in almost most circumstances.

Apart from providing a self contained, reusable concept that can be applied to buttons and key bindings, I find it can produce cleaner code.

I know a lot people prefer to use either inner classes or very large actionPeformed statements with n depths of if-else statements, but for me Action provides an API which is more easily maintained and updatable,

I still use ActionListener in situations where I don't need to display the object that is triggering the actionPerformed event, such as the Swing Timer for example

For me, where you want to maintain a self contained unit of work, which needs to be displayed to the user and/or want to design a series of abstract actionable elements, Action is the preferred method

Upvotes: 2

Related Questions