开发者

How does an ActionListener work?

I have an idea of how to use action lis开发者_高级运维teners 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?


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.


There is an event loop that is implemented into core of AWT. It receives all events and sends them to appropriate listeners.


The tutorial explains how they work fairly well: http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

I think the UI implementation of the JComponent will fire (call) all registered events upon the user interacting with it (I think).

For example, when a user clicks a JButton, the button (or it's ui, or some other internal handler) will lookup all registered ActionListeners and call their actionPerformed(...) methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜