使用 SWT 显示父模式对话框 [英] Display parent modal dialog with SWT

查看:27
本文介绍了使用 SWT 显示父模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AWT/Swing 允许显示应用程序模式(阻止整个应用程序)和父模式(仅阻止父级)对话框.我怎样才能用 SWT 达到同样的效果?

AWT/Swing allows to show application modal (blocking the whole application) and parent modal (blocking only the parents) dialogs. How can I achieve the same with SWT?

推荐答案

为了阻塞整个应用程序,你可以创建对话框Shell,样式为SWT.APPLICATION_MODAL,打开它,然后泵送 UI 事件,直到 shell 被释放:

In order to block the whole application, you can create the dialog Shell with the style SWT.APPLICATION_MODAL, open it, and then pump the UI events until the shell is disposed:

Display display = Display.getDefault();
Shell dialogShell = new Shell(display, SWT.APPLICATION_MODAL);
// populate dialogShell
dialogShell.open();
while (!dialogShell.isDisposed()) {
    if (!display.readAndDispatch()) {
        display.sleep();
    }
}

如果您只想阻止对父级的输入,请尝试使用样式 SWT.PRIMARY_MODAL,尽管 Javadocs 指定(对于其他模态样式)这是一个提示;即,不同的 SWT 实现可能不会完全以相同的方式处理它.同样,我不知道有哪个实现会尊重 SWT.SYSTEM_MODAL 样式.

If you want to block input only to the parent, try using the style SWT.PRIMARY_MODAL, though the Javadocs specify (as for the other modal styles) that this is a hint; i.e., that different SWT implementations may not exactly handle it the same way. Likewise, I don't know of an implementation that would honor the SWT.SYSTEM_MODAL style.

更新:回复第一条评论

如果您同时打开了两个或多个主要模式,则在模式关闭之前,您不能使用这些技巧来泵送事件,因为它们可以按任何顺序关闭.代码将运行,但在当前对话框关闭以及在其之后打开的所有其他此类对话框之后,将在 while 循环后恢复执行.在这种情况下,我会在每个对话框上注册一个 DisposeListener 以在它们关闭时获得回调.像这样的:

If you have two or more primary modals open at the same time, you cannot use the tricks to pump the events until the modal is closed, as they could be closed in any order. The code will run, but execution will resume after the while loop after the current dialog is closed and all other such dialogs that have been opened after it. In this case, I would register a DisposeListener on each dialog to get a callback when they are closed. Something like this:

void run() {
    Display display = new Display();
    Shell shell1 = openDocumentShell(display);
    Shell shell2 = openDocumentShell(display);

    // close both shells to exit
    while (!shell1.isDisposed() || !shell2.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

Shell openDocumentShell(final Display display) {
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FillLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Open Modal Dialog");
    button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Button pressed, about to open modal dialog");
            final Shell dialogShell = new Shell(shell, SWT.PRIMARY_MODAL | SWT.SHEET);
            dialogShell.setLayout(new FillLayout());
            Button closeButton = new Button(dialogShell, SWT.PUSH);
            closeButton.setText("Close");
            closeButton.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    dialogShell.dispose();
                }
            });
            dialogShell.setDefaultButton(closeButton);
            dialogShell.addDisposeListener(new DisposeListener() {
                @Override
                public void widgetDisposed(DisposeEvent e) {
                    System.out.println("Modal dialog closed");
                }
            });
            dialogShell.pack();
            dialogShell.open();
        }
    });
    shell.pack();
    shell.open();
    return shell;
}

这篇关于使用 SWT 显示父模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆