Skip to content

DOM Event listener in GWT

2011 July 13
tags: ,
by Sven Busse

Juten Tach,

should you be scratching your head like i did, when trying to understand, how you would add an event listener to simple DOM elements in GWT, here’s how i do it currently:

Element myDOMElement = Document.get().getElementById("someDOMElement");
Event.setEventListener(myDOMElement, new EventListener() {
 
	@Override
	public void onBrowserEvent(Event event) {
		// TODO Do crazy stuff here
	}
});
Event.sinkEvents(myDOMElement, Event.ONCLICK);

Note the last line here. Only once you’ve called sinkEvents(), do you really get the events for the particular event type(s).

I find this much more usable, than the nasty Event.addNativePreviewHandler() method, that they talk about in the GWT documentation, because you only get the event, you’re interested in.

In general it appears to me, that GWT is more geared towards using their components (or widgets), than working with the DOM directly.

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

*