Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Event Listeners & Handlers in Java?

In general terms of java, there are listeners & handlers for events.
I mean I use them unknowingly, just whichever is available in the API.

My question is, in what case do we use listeners and in what case do we use handlers for events?

What's the difference between them? Characteristics??

I've searched for reasons and I couldn't find a proper explanation for Java.

like image 772
jlc488 Avatar asked Jan 18 '11 14:01

jlc488


People also ask

What are the different types of events of listeners?

Here are some of the most common event types and event names: Mouse Events: click, dblclick, mousedown, mouseup, contextmenu, mouseout, mousewheel, mouseover. Touch Events: touchstart, touchend, touchmove, touchcancel. Keyboard Events: keydown, keyup, keypress.

What is the difference between event and listener in Java?

What are the differences between an event listener interface and an event adapter class in Java? An EventListener interface defines the methods that must be implemented by an event handler for a particular kind of an event whereas an Event Adapter class provides a default implementation of an EventListener interface.

What's the difference between event handlers & addEventListener in JS?

There's no difference; it's just different terminology for the same thing. There are different ways of associating functions with DOM elements for the purpose of event handling, that's all.

What is difference between event and EventHandler?

The event is the thing that RAISES an event, to which something will subscribe. The EventHandler is the thing that HANDLES an event - i.e. it specifies the method that is used to subscribe to the event.


2 Answers

There's no formally defined difference between listeners and handlers. Some people would probably argue that they are interchangeable. To me however, they have slightly different meaning.

A listener is an object that subscribes for events from a source. Cf. the observer pattern. Usually you can have many listeners subscribing for each type of event, and they are added through addXyzListener methods.

Example: The MouseListener in the Java API.

A handler is an object that is responsible for handling certain events. A typical scenario would be to provide a handler for a specific event/task as an argument to a constructor, or set the handler through a setXyzHandler method. In other words, you usually have one handler for each type of event.

Example: The MemoryHandler in the Java API.

like image 188
aioobe Avatar answered Sep 27 '22 19:09

aioobe


The most basic difference is the association

  • Listener is associated with Event Source (Ex: key board)
  • Handler is associated with an Event (Ex: keydown)

Generally speaking, there will only one central Handler Manager which manages all the events, while in case of Listener each Entity which wants to listen, will have to manage their own Collection of listeners

like image 43
Swapnil Avatar answered Sep 27 '22 19:09

Swapnil