3
votes

According to another discussion here, I try to open a modal view like so:

public void widgetSelected(SelectionEvent e) {

    final Shell dialogShell = new Shell(ApplicationRunner.getApp()
            .getShell().getDisplay(), SWT.PRIMARY_MODAL | SWT.SHEET);

    dialogShell.setLayout(new FillLayout());

    Button closeButton = new Button(dialogShell, SWT.PUSH);
    closeButton.setText("Close");
    closeButton.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            dialogShell.dispose();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent arg0) {
            // TODO Auto-generated method stub

        }
    });
    dialogShell.setDefaultButton(closeButton);
    dialogShell.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            System.out.println("Modal dialog closed");
        }
    });
    dialogShell.pack();
    dialogShell.open();
}

It opens the desired window, but it's not modal. I can access the main shell and open another instance of the same modal dialog. Can anybody point me in the right direction?

Thanx, Marcus

2

2 Answers

8
votes

I would highly recommend creating your own JFace Dialog by extending org.eclipse.jface.dialogs.Dialog rather than creating your own shell with buttons. Here is a really good tutorial on this.

Within the contructor you can call setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL); which will make this dialog completely modal, if you call the constructor with your main shell as the parameter. Like this:

public CheckboxDialog(Shell parentShell) {
    super(parentShell);
    setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL);
    setBlockOnOpen(true);
}

where parentShell is the main shell of your GUI.

0
votes

I had this problem today, when creating a pop-up window defined in a separate class.

I was using something like popup_shell = new Shell(Display.getCurrent(), SWT.PRIMARY_MODAL | SWT.DIALOG_TRIM) inside the constructor for the new window.

If instead I pass in the shell from the parent window, like so:

public PopupWindow(Shell main_shell)
{
    popup_shell = new Shell(main_shell, SWT.PRIMARY_MODAL | SWT.DIALOG_TRIM);
}

then it works properly.

My guess is that both ApplicationRunner.getApp().getShell().getDisplay() and Display.getCurrent() result in a completely new shell, unconnected to the parent window, so primary_modal has no effect.