通过单击外部关闭SWT shell

adi*_*eag 5 java swt dialog jface

我有这个JFace对话框:

setShellStyle(SWT.APPLICATION_MODAL | SWT.CLOSE);
setBlockOnOpen(false);
Run Code Online (Sandbox Code Playgroud)

是否可以通过单击对话框外部的某个位置来使其关闭?也许像在整个屏幕上监听click事件,然后检测它是否在对话框之外,然后关闭。

Rüd*_*ann 4

您可以将侦听器附加到对话框的SWT.Deactivate底层。Shell

要附加侦听器,您可以Window::configureShell像这样覆盖:

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

这里有一个独立的 SWT 示例来说明简单的机制:

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();
Run Code Online (Sandbox Code Playgroud)