What are BrowserEvent and NativeEvent for GWT?

What is a browser event? For example, if I have my own widget that contains a single button that overrides onBrowserEvent. The onBrowserEvent method is called only when the button is clicked. I thought this event was triggered for all events like onMouseOver, onMouseOut, KeyPressed, etc.

Also, when you add DomHandler to make the widget listen for an event that is not supported by default. The docs say: "Adds a custom event handler to the widget and absorbs the corresponding custom event." What do you mean by the action of a native event in this context?

thanks

+7
source share
1 answer

GWT has a concept of sunken events. All streaming events, but only those, are EventListener onBrowserEvent .

At the lowest level, you attach an EventListener to an Element using DOM.setEventListener and accept events from DOM.sinkEvents (or most recently, DOM.setBitlessEvents ). To avoid memory leaks (especially - not only in old IEs), before the page is unloaded, you must set the Element EventListener to null .

A Widget is an EventListener and handles some of these things for you: it automatically calls the DOM.setEventListener in its onAttach and onDetach , and it tracks streaming events: its sinkEvents is additive to make it easier to work with, and therefore has an analog unsinkEvents .

Event handlers were added later in GWT 1.6, and addDomHandler (and more recently addBitlessDomHandler ) automatically calls sinkEvents with the appropriate values โ€‹โ€‹(taken from the DomEvent.Type argument passed as the argument); and to do all this work, the default behavior of onBrowserEvent in widgets is to send events to registered handlers.

The reason for these painless options is that the events were originally called int constants in the bit field, but in browsers there are more and more events, so the GWT started to run out of bits. The painless options are available only for browsers that do not leak, as widgets do not track which events were registered in order to unregister them from onDetach , unlike bit events.

All of these new DomEvent (with their handlers), starting with GWT 1.6, are wrappers around NativeEvent . There are two types of GwtEvent s: native ( DomEvent s), which are dispatched by the browser, and logical, which are dispatched by GWT themselves and are not displayed on events at the DOM level. addDomHandler applies only to DomEvent s, aka native events.

Completion: when you exit the text field that you just changed, the browser dispatches a change event. If the TextBox widget lost this event, its onBrowserEvent will be called with the Event (which is just an inherited subclass of NativeEvent ) representing this event. By default, the onBrowserEvent implementation creates a ChangeEvent and sends it to the registered ChangeHandler s.

+13
source

All Articles