Easy firing click event from code in GWT

Firing native events in GWT can be a real pain. If you need to fire a click event when the user press ENTER (or any other key that you want to work like CLICK) on any custom-made element, theoretically you should use:

*DomEvent.fireNativeEvent(com.google.gwt.dom.client.NativeEvent, com.google.gwt.event.shared.HasHandlers) * Minimal code (without required parameters) linked with this approach: NativeEvent event = Document.get().createChangeEvent();
DomEvent.fireNativeEvent(event, this);
public final NativeEvent createClickEvent(int detail, int screenX, int screenY, int clientX, int clientY, boolean ctrlKey, boolean altKey, boolean shiftKey, boolean metaKey) This is the place where native JSNI approach is much more clearer: public static native void click(Element element)/*-{
element.click();
}-*/;

This approach is very useful for custom made buttons that for some reason can not extend basic Button class. In any other case try not to simulate “user actions” in your handlers. Launch designated business methods instead.

You May Also Like

Mock Retrofit using Dagger and Mockito

Retrofit is one of the most popular REST client for Android, if you never use it, it is high time to start. There are a lot of articles and tutorial talking about Retrofit. I just would like to show how to mock a REST server during develop of app and i...Retrofit is one of the most popular REST client for Android, if you never use it, it is high time to start. There are a lot of articles and tutorial talking about Retrofit. I just would like to show how to mock a REST server during develop of app and i...

OSGi Blueprint visualization

What is blueprint?Blueprint is a dependency injection framework for OSGi bundles. It could be written by hand or generated using Blueprint Maven Plugin. Blueprint file is only an XML describing beans, services and references. Each OSGi bundle could hav...