Reputation: 16367
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
Upvotes: 23
Views: 62791
Reputation: 657
If the target is a button you can just call its click()
method. Otherwise you can do something 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);
}
Upvotes: 4
Reputation: 7779
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();
}-*/;
Upvotes: 17
Reputation: 211
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.
Upvotes: 21
Reputation: 553
Please notice that the KeyPressHandler doesn't work in Firefox for special keys like ENTER, you would need to use the KeyUp or KeyDown
http://code.google.com/p/google-web-toolkit/issues/detail?id=5558#c6
Upvotes: 2
Reputation: 13519
The method described by DLH should work and except for the detail argument (which I have no idea what's it for) you have the other arguments available in the KeyPressEvent.
Another possible solution is to call the native JavaScript click()
on the element. I've done this in a Button widget (Which is available as open source). See click()
method in the following 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 class, that implements the browser specific versions of the click method.
To use this method, you could simply add the jar file provided with cobogw to your project, include the Event.gwt.xml
and call Event2.fireClickEvent(getElement());
in your method or only use the code from the classes Event2 and Event in your own project
This solution also allows you the programmatically fire a click event.
Also take a look at the onBrowserEvent
implementation in the Button class mentioned above , since it handles the key event in a similar way you want, and works around the problem of the firing of multiple key events, when you only want to generate 1 click event.
Upvotes: 2
Reputation: 2821
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)
Upvotes: 19
Reputation: 494
Maybe ClickListenerCollection.fireClick() does what you want. I haven't used it though
Sorry, that is deprecated and maybe not even useful. I guess you shoul look at widget.delegateEvent instead.
Upvotes: 1