Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close Dialogbox when click ESCAPE(ESC) in GWT

You can override the onPreviewNativeEvent() method of the DialogBox class:

public void onModuleLoad() {
    ExtendedDialogBox dialog = new ExtendedDialogBox();

    dialog.add(new Label("some content"));

    dialog.show();
}

private class ExtendedDialogBox extends DialogBox {

    @Override
    protected void onPreviewNativeEvent(NativePreviewEvent event) {
        super.onPreviewNativeEvent(event);
        switch (event.getTypeInt()) {
            case Event.ONKEYDOWN:
                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
                    hide();
                }
                break;
        }
    }
}

@Override
 public boolean onKeyDownPreview(char key, int modifiers) {
     switch (key) {
       case KeyCodes.KEY_ESCAPE:
         hide();
         break;
     }

     return true;
 }