close Dialogbox when click ESCAPE(ESC) in GWT

纵饮孤独 提交于 2019-12-18 12:08:22

问题


How can I add something like addCloseHandler to dialogbox that to close when click the ESC key?


回答1:


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;
        }
    }
}



回答2:


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

     return true;
 }


来源:https://stackoverflow.com/questions/4301859/close-dialogbox-when-click-escapeesc-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!