What is ActionListener for? [closed]

closed . This question needs to be more objective and is not currently accepting answers.

Want to improve this question? update the question to focus on just one problem when edit it .

Closed 1 year ago .

improve this question

I'm starting to study Java.

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

Because I'm having a little trouble understanding,so I would like practical examples of how to use such a method.

Author: Luiz Augusto, 2019-07-30

1 answers

Listener, in java, is a way to implement the"Observer" Project pattern pattern.

Therefore, the Listener serves to listen to what happens in an object and if there is some change of state, other objects are warned. That is, its function is to "listen" to changes that occur in the object being monitored.

That 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 fired. For example, in the JButton component it is possible to register an ActionListener for it, so that upon a click, an ActionEvent is performed, thus calling the actionperformed method of the ActionListener, consequently performing the routine that you have defined for the method.

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

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

 1
Author: Diego Aguiar, 2019-07-30 22:33:07