我的程序打开一个对话框(在给定的 shell 上):
new FileDialog(shell).open();
我的问题是:我如何(在程序的另一部分)关闭给定的所有打开的对话框 外壳?
private void closeDialogs(Shell shell) {
// how to close open dialogs?
}
下面是最小的测试用例,一键打开对话框。另一个按钮应该 关闭当前 shell 的所有打开的对话框。
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Testcase");
shell.setLayout(new FillLayout());
final Button button = new Button(shell, SWT.PUSH);
button.setText("Open Dialog");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
new NonModalDialog(shell).open();
}
});
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Close open dialogs");
button2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// How to close all open dialogs of the given shell?
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
最佳答案
打开对话框时,您必须将它们收集在列表中。
单击“关闭”按钮后,浏览列表并关闭对话框。
注意:当任何对话框是模态时,这将不起作用。模式对话框将接管事件循环并忽略任何不针对其自身的事件,因此单击按钮将是不可能的。
关于java - 如何关闭给定 SWT shell 的所有打开的对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22167909/