java - 如何在不激活 shell 的情况下打开它?

标签 java shell swt eclipse-rcp jface

我需要知道是否有任何方法可以打开 shell 而不使其处于 Activity 状态,即使我单击其中的控件也是如此。解释我需要什么的最好方法是向您展示这个小例子。我需要保持第一个 shell 处于 Activity 状态,即使我单击第二个 shell 或其包含的任何小部件。

public class TestMeOut
{
    public static void main(final String[] args)
    {

        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        final Shell shell2 = new Shell(shell);
        shell2.setLayout(new GridLayout());

        final Button btn = new Button(shell, SWT.PUSH);
        final Button btn2 = new Button(shell2, SWT.PUSH);

        btn.setText("Test me");
        btn2.setText("I steal focus");

        btn.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(final SelectionEvent e)
            {
                shell2.setVisible(true);
            }
        });

        btn2.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(final SelectionEvent e)
            {
                shell2.setVisible(false);
            }
        });

        shell.pack();
        shell.open();

        shell.addShellListener(new ShellListener()
        {

            public void shellIconified(final ShellEvent e)
            {
            }

            public void shellDeiconified(final ShellEvent e)
            {
            }

            public void shellDeactivated(final ShellEvent e)
            {
                System.out.println("Deactivated! This isn't supposed to happen.");
            }

            public void shellClosed(final ShellEvent e)
            {
            }

            public void shellActivated(final ShellEvent e)
            {
                System.out.println("Activated!");
            }
        });

        shell2.pack();
        shell2.open();
        shell2.setVisible(false);

        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

提前谢谢您!

最佳答案

您可以更改第二个 Shell 的样式并使用 SWT.APPLICATION_MODAL ,这将确保您无法与父 shell 交互,除非您关闭子 shell:

final Shell shell2 = new Shell(shell, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);

The modality of an instance may be specified using style bits. The modality style bits are used to determine whether input is blocked for other shells on the display. The PRIMARY_MODAL style allows an instance to block input to its parent. The APPLICATION_MODAL style allows an instance to block input to every other shell in the display. The SYSTEM_MODAL style allows an instance to block input to all shells, including shells belonging to different applications.

在这种情况下,甚至 SWT.PRIMARY_MODAL 就足够了。


更新

另一方面,如果您不希望父级在单击子级时失去焦点,只需禁用子级即可:

shell2.setEnabled(false);

关于java - 如何在不激活 shell 的情况下打开它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120040/

相关文章:

linux - 如何显示文件的第二行并将字符转换为大写

shell - awk shell 参数 FILENAME 与 ARGV

java - 在shell脚本中设置环境变量并在Java程序中访问

java - SWT - 关闭不同类的对话框窗口

JAVA:将替代(||)放入循环中?

java - 从具有许多相同元素名称的 XML 模式生成时如何避免巨大的 Java 文件

java - 让 SWT StyledText 小部件始终滚动到其末尾

java - 将 SWT 小部件添加到 swing JPanel

java - HashSet 中的问题 - 保存对象

java - prb 和我的 json 响应