无法关闭对话框

时间:2019-01-29 06:28:35

标签: eclipse swt jface e4

我正在尝试在swt中打开一个对话框,在该对话框中,我将覆盖createDialogArea方法以自定义对话框。在这种方法中,我尝试打开MessageDialog。打开该对话框如果openQuestion消息返回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)

1 个答案:

答案 0 :(得分:1)

您不能直接在close中调用createDialogArea,JFace对话框代码希望对话框在创建对话框之前一直保持有效。

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

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

但是,最好先显示该消息框,然后再尝试创建对话框。