如何在对话框中添加类似addCloseHandler的内容,以便在单击ESC键时关闭?
答案 0 :(得分:37)
您可以覆盖onPreviewNativeEvent()
类的DialogBox
方法:
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;
}
}
}
答案 1 :(得分:1)
@Override
public boolean onKeyDownPreview(char key, int modifiers) {
switch (key) {
case KeyCodes.KEY_ESCAPE:
hide();
break;
}
return true;
}