What is better to use: Action vs ActionListener?

2019-01-25 11:29发布

问题:

Myself, I always use ActionListener as swing event-handler (e.g. button-click), and it is the most common listener I've seen in most swing applications.

However, Some Swing professionals here in stackoverflow often advise to use Action rather than ActionListener. What benefits I get from doing so?

回答1:

An Action is preferred if you need to share functionality across components. From the docs

if you have two or more components that perform the same function, consider using an Action object to implement the function.

but also says

An Action object is an action listener that provides not only action-event handling, but also centralized handling of the state of action-event-firing components such as tool bar buttons, menu items, common buttons, and text fields. The state that an action can handle includes text, icon, mnemonic, enabled, and selected status.