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

Context menu or Action buttons ?

Recently I was drawn into one of those UI "religious" disputes that has no easy answers and usually both sides are right. One of our web developers was trying out new web tech (with pretty rich widget library) and started to question himself about some basic usability decisions. The low level problem in this case is usually brought to "which widget should I use ?". I'm not fond of bringing the usability problems to questions: Should I use Tabs over Menu ? Or should I use Context menu instead of buttons panel ? But sometimes if time is crucial factor and other usability levels are by default not addressed at all - better developer that asks those basic questions than developer that do not question himself at all.