My question is, what's the different between event preview and event handler in GWT.
There is a callback function boolean onEventPreview(Event event)
for event preview and a callback function void onBrowserEvent(Event event)
as well. They are pretty similar, so what's the different between them? Especially when should I use the event preview at all when the event handler works perfect?
thanks
DOM.addEventPreview(EventPreview preview)
lets you place an event preview on top of the event stack, which is called before any onBrowserEvent(Event event)
is fired. This way you can place some logic before the event firing takes place. You can even prevent the event from firing by returning false. For example below example prevents the browser from reacting to mousemove and mousedown events.(Click and drag an image, browser won't drag an outline of image)
DOM.addEventPreview(new EventPreview() {
@Override
public boolean onEventPreview(Event event) {
switch (DOM.eventGetType(event)){
case Event.ONMOUSEDOWN:
case Event.ONMOUSEMOVE:
event.preventDefault();
}
return true;
}
});
Just a reminder, adding eventPreviews this way is depreciated. Correct way to do it is to use Event.addNativePreviewHandler(NativePreviewHandler handler)
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