I am using GWT. I started adding events to my widgets by adding EventHandlers.
Event handler sample code:
widget.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// do something
}
});
I then found there is another way to handle events using sinkEvents().
Sink events sample code (from this website):
{
...
sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONMOUSEOVER |Event.ONMOUSEOUT)
...
}
public void onBrowserEvent(Event event) {
Element td = getEventTargetCell(event);
if (td == null) return;
Element tr = DOM.getParent(td);
switch (DOM.eventGetType(event)) {
case Event.ONMOUSEDOWN: {
// do something
break;
}
case Event.ONMOUSEUP: {
// do something
break;
}
case Event.ONMOUSEOVER: {
// do something
break;
}
case Event.ONMOUSEOUT: {
// do something
break;
}
}
}
The minor difference between event handlers and event listeners is easy to understand: You can only have one event handler for a specific event type, but you can add multiple event listeners for it. Let’s see what happens if you try to add two onclick event handlers to the same object:
This way, anytime a new meetup is scheduled, you get alerted. That is event handling! The "event" here is a new JS meetup. When a new meetup is posted, the website meetup.com catches this change, thereby "handling" this event.
If useCapture is set to false, the event handler is in the bubbling phase. Otherwise it's in the capture phase. The order of the phases of the event depends on the browser.
is output into the console. The other option to handling events is by using event listeners. An event listener is something you assign to an object. As the name suggests, the event listener listens for events and gets triggered when an event occurs. Let’s repeat the previous example by assigning an event listener to a button to listen for clicks.
I'm not GWT expert, but this is what I gather from looking at the GWT source:
All EventHandlers (eventually) call addDomHandler(..)
which calls sinkEvents()
. sinkEvents()
is lower-level and is a browser-abstraction wrapper around native javascript event handling.
EventHandlers are build on top of sinkEvents so they provide all functionality that sinkEvents does.
But usage-wise they are different: with EventHandlers you can register for different event types with different event handlers that can reside in different classes. Events will be automatically routed to appropriate handlers. With sinkEvents you can register for different event types (via an int, not type-safe), but always this widget's onBrowserEvent(event)
will be called.
EventHandlers add certain overhead. It's debatable if this matters at all.
EventHandlers are a type-safe way to add and remove event handlers and an automatic way for events to be routed to the registered handlers of your choice. If you use GWT Widgets than you should use EventHandlers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With