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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With