JFace: Create a dialog on top of the exists top dialog

61 Views Asked by At

I have a top-level dialog called dialogA with the following options.

public class DialogA extends Dialog {

  public DialogA() {
    super(null);
    setShellStyle(SWT.DIALOG_TRIM | SWT.ON_TOP);
  }

}

Now I want to create a new dialog called dialogB on top of the dialogA, how can I do it?

My attempts:

Attempt 1:

public class DialogB extends Dialog {

  public DialogB(Shell parentShell) {
    super(parentShell);
    setShellStyle(SWT.DIALOG_TRIM | SWT.ON_TOP);
  }

}

Open:

DialogA dialogA = new DialogA();
dialogA.setBlockOnOpen(false);
dialogA.open();
DialogB dialogB = new DialogB(dialogA.getShell());
dialogB.open();

Result: The dialogB is on top of the dialogA, but the dialog_trim style disappeared.

Attempt 2:

public class DialogB extends Dialog {

  public DialogB() {
    super(null);
    setShellStyle(SWT.DIALOG_TRIM | SWT.ON_TOP);
  }

}

Open:

DialogA dialogA = new DialogA();
dialogA.setBlockOnOpen(false);
dialogA.open();
DialogB dialogB = new DialogB();
dialogB.open();

Result: dialogB is under dialogA.

Could someone help me please?

1

There are 1 best solutions below

4
greg-449 On

I can't reproduce the trim disappearing, that might be specific to your OS (I'm on macOS).

You should not need SWT.ON_TOP or to change the dialog style at all.

It is more normal to open to second dialog from within the first dialog.

The following works for me:

DialogA dialogA = new DialogA(null);
dialogA.open();
public class DialogA extends Dialog
{
  public DialogA(final Shell parent)
  {
    super(parent);
  }

  @Override
  protected void configureShell(final Shell newShell)
  {
    newShell.setText("Title A");

    super.configureShell(newShell);
  }

  @Override
  protected Control createDialogArea(final Composite parent)
  {
    Composite area = (Composite)super.createDialogArea(parent);

    new Label(area, SWT.LEAD).setText("label A");

    // Open DialogB once this dialog has benn displayed
    Display.getCurrent().asyncExec(() ->
      {
        DialogB dialogB = new DialogB(getShell());
        dialogB.open();
      });

    return area;
  }
}
public class DialogB extends Dialog
{
  public DialogB(final Shell parent)
  {
    super(parent);
  }

  @Override
  protected void configureShell(final Shell newShell)
  {
    newShell.setText("Title B");

    super.configureShell(newShell);
  }

  @Override
  protected Control createDialogArea(final Composite parent)
  {
    Composite area = (Composite)super.createDialogArea(parent);

    new Label(area, SWT.LEAD).setText("label B");

    return area;
  }
}