My goal is to activate button's onClick(..) method if I press Enter key (KEY_ENTER) on the keyboard. For this purpose I am trying the following code but I am getting an exception which says:
com.google.gwt.event.shared.UmbrellaException: Exception caught: com.google.gwt.user.client.ui.Button cannot be cast to com.google.gwt.event.dom.client.ClickHandler
Code:
returnKeyHandler = new KeyDownHandler() {
@Override
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
Window.alert("Enter key pressed!!");
((ClickHandler) button).onClick(null);
}
}
};
Button button = new Button();
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("blah blah");
//blah blah..
}
});
TextBox textBox = new TextBox();
textBox.addKeyDownHandler(returnKeyHandler);
I can understand the exception but unable to find the solution.