Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Event Preview vs Event Handler

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

like image 716
cn1h Avatar asked Feb 24 '23 06:02

cn1h


1 Answers

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)

like image 143
pistolPanties Avatar answered Mar 04 '23 01:03

pistolPanties