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

查看:177
本文介绍了使用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天全站免登陆