java - 如何关闭给定 SWT shell 的所有打开的对话框?

标签 java swt

我的程序打开一个对话框(在给定的 shell 上):

new FileDialog(shell).open();

我的问题是:我如何(在程序的另一部分)关闭给定的所有打开的对话框 外壳?

private void closeDialogs(Shell shell) {
    // how to close open dialogs?
}

下面是最小的测试用例,一键打开对话框。另一个按钮应该 关闭当前 shell 的所有打开的对话框。

enter image description here

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/

相关文章:

java - 等宽窗扇

java - 如何使用 SWT 浏览器查看 PDF 数据字节

java - 我如何获取日期时间戳作为 UTC

java - Spring ldap 身份验证失败错误代码

java - 如何编写一个程序来解决多个问题?

java 使用 file[] 获取多个目录的文件夹大小

java - 将字符串转换为 swt 颜色

java - 在 im4java 中打开 SWT 图像

java - 异常 android.support.multidex.MultiDexApplication 不能投类

JAVA - swt,我们是否必须为每个操作系统重新制作字节码?