开发者

GWT Event Preview vs Event Handler

开发者 https://www.devze.com 2023-03-28 14:41 出处:网络
My question is, what\'s the different between event pr开发者_开发百科eview and event handler in GWT.

My question is, what's the different between event pr开发者_开发百科eview 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)


From the javadoc:

As long as this preview remains on the top of the stack, it will receive all events before they are fired to their listeners. Note that the event preview will receive all events, including those received due to bubbling, whereas normal event handlers only receive explicitly sunk events.

You can return false from onEventPreview to cancel the event, in which case the event handlers will not be fired.

0

精彩评论

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