Debasish Halder
Debasish Halder

Reputation: 185

Unable to close the dialog

I am trying to open a dialog in swt where I Override the createDialogArea method for customise my dialog. In this method I am trying to openQuestion MessageDialog. Open that dialog If openQuestion message return yes otherwise close the dialog before open it. When I click on Cancel on OpenQuestion Dialog I got an exception.

@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)

Upvotes: 0

Views: 534

Answers (1)

greg-449
greg-449

Reputation: 111217

You can't call close directly in createDialogArea, the JFace dialog code expects the dialog to remain valid all the way through creating the dialog.

You can use Display.asyncExec to delay the close until the dialog is open. Replace this.close() with:

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

However it would be better to display the message box before you try to create the dialog in the first place.

Upvotes: 1

Related Questions