[ACCEPTED]-Firing click event from code in gwt-gwt

Accepted answer
Score: 21

I have done this code:

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)        {
    myButton.fireEvent( new GwtEvent<ClickHandler>() {
        @Override
        public com.google.gwt.event.shared.GwtEvent.Type<ClickHandler> getAssociatedType() {
        return ClickEvent.getType();
        }
        @Override
        protected void dispatch(ClickHandler handler) {
            handler.onClick(null);
        }
   });
}

Of course myButton 2 must be final or public cause you are inside 1 another event handler.

Score: 19

I haven't done this for a click event, but 2 I've done change events like this.

NativeEvent event = Document.get().createChangeEvent();
DomEvent.fireNativeEvent(event, this);

The [createClickEvent](http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/dom/client/Document.html#createClickEvent(int,%20int,%20int,%20int,%20int,%20boolean,%20boolean,%20boolean,%20boolean)) method 1 takes a lot more parameters though.

public final NativeEvent createClickEvent(int detail,
                                          int screenX,
                                          int screenY,
                                          int clientX,
                                          int clientY,
                                          boolean ctrlKey,
                                          boolean altKey,
                                          boolean shiftKey,
                                          boolean metaKey)
Score: 17

You can also use a simple JSNI method to 2 do it. Just pass your element [e.g. button.getElement()] to 1 this method:

public static native void clickElement(Element elem) /*-{
    elem.click();
}-*/;
Score: 4

If the target is a button you can just call 2 its click() method. Otherwise you can do something 1 like this:

private void click(HasHandlers handlerSource) {
  NativeEvent event = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
  DomEvent.fireNativeEvent(event, handlerSource);
}
Score: 2

The method described by DLH should work 22 and except for the detail argument (which 21 I have no idea what's it for) you have the 20 other arguments available in the KeyPressEvent.

Another 19 possible solution is to call the native 18 JavaScript click() on the element. I've done this 17 in a Button widget (Which is available as 16 open source). See click() method in the following 15 class: http://code.google.com/p/cobogw/source/browse/trunk/widgets/src/main/java/org/cobogw/gwt/user/client/ui/Button.java), which calls a specific Event2 14 class, that implements the browser specific 13 versions of the click method.

To use this 12 method, you could simply add the jar file 11 provided with cobogw to your project, include 10 the Event.gwt.xml and call Event2.fireClickEvent(getElement()); in your method or only use 9 the code from the classes Event2 and Event 8 in your own project

This solution also allows 7 you the programmatically fire a click event.

Also 6 take a look at the onBrowserEvent implementation in the 5 Button class mentioned above , since it 4 handles the key event in a similar way you 3 want, and works around the problem of the 2 firing of multiple key events, when you 1 only want to generate 1 click event.

Score: 2

Please notice that the KeyPressHandler doesn't 2 work in Firefox for special keys like ENTER, you 1 would need to use the KeyUp or KeyDown

http://code.google.com/p/google-web-toolkit/issues/detail?id=5558#c6

Score: 1

Maybe ClickListenerCollection.fireClick() does what you want. I haven't used it though

Sorry, that is deprecated and maybe not 2 even useful. I guess you shoul look at widget.delegateEvent 1 instead.

Score: 0

You can just do this:

focusPanel.fireEvent(new ClickEvent(){});

0

More Related questions