无法关闭对话框

Unable to close the dialog

我试图在 swt 中打开一个对话框,我在其中覆盖了 createDialogArea 方法以自定义我的对话框。在这种方法中,我试图打开问题消息对话框。打开那个对话框 如果 openQuestion message return yes 否则在打开之前关闭对话框。当我在 OpenQuestion 对话框中单击取消时出现异常。

@Override
protected Control createDialogArea(Composite parent) {
    private boolean m_firstQuery = true;
    getShell().setText("Test");

    Composite composite = (Composite) super.createDialogArea(parent);
    composite.setLayout(new GridLayout(1, true));

    final Display display = Display.getCurrent();
    boolean moveOn = true;
    if (m_firstQuery) {
         moveOn = MessageDialog.openQuestion(display .getActiveShell(),
                "Do you want to open the dialog?", "Yes/No");
         if (!moveOn) {
             this.close();
         }

        m_firstQuery = false;
    }

    return composite;
}
java.lang.IllegalArgumentException: Argument not valid
 at org.eclipse.swt.SWT.error(SWT.java:4533)
 at org.eclipse.swt.SWT.error(SWT.java:4467)
 at org.eclipse.swt.SWT.error(SWT.java:4438)
 at org.eclipse.swt.widgets.Widget.error(Widget.java:448)
 at org.eclipse.swt.widgets.Widget.checkParent(Widget.java:285)
 at org.eclipse.swt.widgets.Widget.<init>(Widget.java:151)
 at org.eclipse.swt.widgets.Control.<init>(Control.java:110)
 at org.eclipse.swt.widgets.Scrollable.<init>(Scrollable.java:82)
 at org.eclipse.swt.widgets.Composite.<init>(Composite.java:96)
 at org.eclipse.jface.dialogs.Dialog.createButtonBar(Dialog.java:648)
 at org.eclipse.jface.dialogs.Dialog.createContents(Dialog.java:769)
 at org.eclipse.jface.window.Window.create(Window.java:426)
 at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1096)
 at org.eclipse.jface.window.Window.open(Window.java:783)

您不能在 createDialogArea 中直接调用 close,JFace 对话框代码希望对话框在创建对话框的整个过程中保持有效。

您可以使用 Display.asyncExec 延迟关闭,直到对话框打开。将 this.close() 替换为:

display.asyncExec(() -> close());

不过,最好先显示消息框,然后再尝试首先创建对话框。