Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firing click event from code in gwt

Tags:

gwt

I have created a custom widget in gwt which extends the composite.I am using focus panel in that.For FocusPanel I added ClickHandler.Then I have added keyboard listner.Now on press of Enter key it should trigger click event.Can any one help me to trigger click event by using code in GWT?

focusPanel.addKeyPressHandler(new KeyPressHandler() {
            public void onKeyPress(KeyPressEvent event) {
                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)        {
                    // TODO call onClick() method
                }
            }
        });

Thanks

like image 454
DonX Avatar asked Aug 06 '09 11:08

DonX


3 Answers

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 must be final or public cause you are inside another event handler.

like image 145
Rod Avatar answered Nov 10 '22 12:11

Rod


I haven't done this for a click event, but 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 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)
like image 44
DLH Avatar answered Nov 10 '22 14:11

DLH


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

public static native void clickElement(Element elem) /*-{
    elem.click();
}-*/;
like image 17
11101101b Avatar answered Nov 10 '22 13:11

11101101b