开发者

close Dialogbox when click ESCAPE(ESC) in GWT

How can 开发者_如何学JAVAI add something like addCloseHandler to dialogbox that to close when click the ESC key?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜