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

Loops performance in Groovy

IntroductionIn the 2018 Advent of Code challenged I solved all the puzzles in Groovy. It is pretty obvious, that choosing good data structure is the most important to obtain performant solution. However, the way we iterate over those structures is also...