What’s the Actionlistener for?

Asked

Viewed 544 times

0

I’m starting to study Java.

Can someone explain to me, in detail, in the most complete and simple way possible, what good is the method actionListener java (da lib swing).

'Cause I’m having a little trouble understanding,.

1 answer

1

Listener, in java, is a way to implement the "Observer" project standard.

Thus, the Listener serves to listen to what happens in one object and there is some change of state, another (s) objects is (are) warned(s). That is, its function is to "listen" to changes that occur in the object being monitored.

That being said, Actionlistener is a Listener interface that requires the implementation of only one method, actionPerformed(Actionevent e), which is responsible for performing a certain action when an Actionevent (any event) is triggered. For example, in the Jbutton component it is possible to register an Actionlistener for it, so that when it is clicked, an Actionevent is performed, thus calling the actionPerformed method of Actionlistener, consequently performing the routine you defined for the method.

Finally, Actionlistener is used to hear actions performed on certain components (such as button clicks) and make some logical decision based on this action. Thus, making it possible to assign functionality to buttons or some other component.

As a suggested implementation, see this link: https://www.devmedia.com.br/java-listeners-trabalhando-com-actionlistener-e-keylistener-em-java/31850

Browser other questions tagged

You are not signed in. Login or sign up in order to post.