Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an ActionListener work?

I have an idea of how to use action listeners and implementing them but I was wondering if anyone could tell me how do they listen to the events? Is there some kind of polling mechanism?

like image 718
aditya_gaur Avatar asked Feb 03 '11 11:02

aditya_gaur


People also ask

How does ActionListener work in Java?

To determine where the user clicked on the screen, Java provides an interface called "ActionListener" through which we determine where the user clicked and generates an event to perform several tasks, like calculation, print a value, print a specific character, etcetera using a button.

What is the method for event ActionListener?

The ActionListener interface is found in java. awt. event package. It has only one method: actionPerformed().

What is the purpose of ActionListener interface in swing?

The class which processes the ActionEvent should implement this interface. The object of that class must be registered with a component. The object can be registered using the addActionListener() method.

What does ActionListener mean?

ActionListener in Java is a class that is responsible for handling all action events such as when the user clicks on a component. Mostly, action listeners are used for JButtons. An ActionListener can be used by the implements keyword to the class definition.


1 Answers

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. Because everything in your code runs off a single thread (the event loop) you don't have to worry about synchronizing between different events - so your code is threadsafe.

like image 122
Joel Avatar answered Nov 03 '22 01:11

Joel