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?
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."
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.
In JSF, “Action Events” are fired by clicking on a button or link component, e.g h:commandButton or h:commandLink.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With