Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better to use: Action vs ActionListener?

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?

like image 650
Eng.Fouad Avatar asked Feb 24 '13 13:02

Eng.Fouad


People also ask

What is the difference between action and ActionListener?

What is the difference between action and actionListener on a component? From the "core JavaServerFaces" book: "In a nutshell, actions are designed for business logic and participate in navigation handling, whereas action listeners typically perform user interface logic and do not participate in navigation handling."

Why do we use ActionListener?

You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field.

What is JSF action?

In JSF, “Action Events” are fired by clicking on a button or link component, e.g h:commandButton or h:commandLink.

How do action listeners work?

Action listeners register for Events using the Observer pattern and they are notified, by the main event loop, of any events they are registered for. So no, it's not a polling (pull) mechanism, but the opposite - a (push) callback. This is an example of 'don't call us, we'll call you' programming.


1 Answers

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.

like image 109
Reimeus Avatar answered Oct 18 '22 08:10

Reimeus