[英]Firing click event from code in 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? 我在gwt中创建了一个自定义小部件,它扩展了复合。我正在使用焦点面板。对于FocusPanel,我添加了ClickHandler。然后我添加了键盘listner。现在按下Enter键它应该触发click事件。可以任何一个帮助我使用GWT中的代码触发click事件?
focusPanel.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
// TODO call onClick() method
}
}
});
Thanks 谢谢
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. 当然myButton必须是final或public,因为你在另一个事件处理程序中。
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. [ createClickEvent
]( http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/dom/client/Document.html#createClickEvent ( createClickEvent
20int,%20int,%20boolean,%20boolean,%20boolean,%20boolean))方法虽然需要更多参数。
public final NativeEvent createClickEvent(int detail,
int screenX,
int screenY,
int clientX,
int clientY,
boolean ctrlKey,
boolean altKey,
boolean shiftKey,
boolean metaKey)
You can also use a simple JSNI method to do it. 您也可以使用简单的JSNI方法来完成它。 Just pass your element [eg button.getElement()] to this method: 只需将您的元素[例如button.getElement()]传递给此方法:
public static native void clickElement(Element elem) /*-{
elem.click();
}-*/;
If the target is a button you can just call its click()
method. 如果目标是按钮,则可以调用其click()
方法。 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);
}
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. DLH描述的方法应该工作,除了详细参数(我不知道它是什么),你有KeyPressEvent中的其他参数。
Another possible solution is to call the native JavaScript click()
on the element. 另一种可能的解决方案是在元素上调用本机JavaScript click()
。 I've done this in a Button widget (Which is available as open source). 我在一个Button小部件(可以作为开源)中完成了这个。 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. 请参阅以下类中的click()
方法: http : //code.google.com/p/cobogw/source/browse/trunk/widgets/src/main/java/org/cobogw/gwt/user/client/ui/ Button.java ),它调用特定的Event2类,实现click方法的浏览器特定版本。
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());
要使用此方法,您只需将随cobogw提供的jar文件添加到项目中,包含Event.gwt.xml
并调用Event2.fireClickEvent(getElement());
in your method or only use the code from the classes Event2 and Event in your own project 在您的方法中或仅使用您自己的项目中的类Event2和Event中的代码
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. 还要看一下上面提到的Button类中的onBrowserEvent
实现,因为它以你想要的类似方式处理键事件,并解决了多个键事件触发的问题,当你只想生成1个单击事件时。
Please notice that the KeyPressHandler doesn't work in Firefox for special keys like ENTER, you would need to use the KeyUp or KeyDown 请注意,对于像ENTER这样的特殊键,KeyPressHandler在Firefox中不起作用,您需要使用KeyUp或KeyDown
http://code.google.com/p/google-web-toolkit/issues/detail?id=5558#c6 http://code.google.com/p/google-web-toolkit/issues/detail?id=5558#c6
Maybe
ClickListenerCollection.fireClick() does what you want.
也许
ClickListenerCollection.fireClick()做你想要的。
I haven't used it though
我没有用它
Sorry, that is deprecated and maybe not even useful. 对不起,这已被弃用,甚至可能没用。 I guess you shoul look at widget.delegateEvent instead. 我想你应该看看widget.delegateEvent。
你可以这样做:
focusPanel.fireEvent(new ClickEvent(){});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.