Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close SWT shell by clicking outside of it

I have this JFace dialog:

setShellStyle(SWT.APPLICATION_MODAL | SWT.CLOSE);
setBlockOnOpen(false);

Is there a way to make it close by clicking somewhere outside the dialog? Maybe something like listening for a click event on the whole screen and detecting if it's outside the dialog, and then closing.

like image 733
adi.neag Avatar asked Jan 26 '18 14:01

adi.neag


1 Answers

You can attach an SWT.Deactivate listener to the underlying Shell of the dialog.

To attach the listener, you could override Window::configureShell like this:

@Override
protected void configureShell(Shell shell) {
  super.configureShell(shell);
  shell.addListener(SWT.Deactivate, event -> shell.close());
}

And here a standalone SWT example to illustrate the bare mechanism:

Display display = new Display();
Shell parentShell = new Shell(display);
parentShell.setSize(500, 500);
parentShell.open();
Shell shell = new Shell(parentShell);
shell.addListener(SWT.Deactivate, event -> shell.close());
shell.setSize(300, 300);
shell.setText("Closes on Deactivate");
shell.open();
while (!parentShell.isDisposed()) {
  if (!display.readAndDispatch())
    display.sleep();
}
display.dispose();
like image 199
Rüdiger Herrmann Avatar answered Oct 14 '22 17:10

Rüdiger Herrmann