如何在不激活shell的情况下打开shell? [英] How can i open a shell without making it active?

查看:212
本文介绍了如何在不激活shell的情况下打开shell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to know if there is any way to open a shell and not make it active, even if i click a control in it. The best way to explain what i need is to show you this little example. I need to keep the first shell active, even if i click the second one, or any widget that it contains.

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:

You can change the style of your second Shell and use SWT.APPLICATION_MODAL which will make sure that you cannot interact with the parent shell unless you close the child shell:

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




可以使用样式位指定实例的形态。模态样式位用于确定输入是否被阻止显示器上的其他外壳。 PRIMARY_MODAL 样式允许实例阻止对其父级的输入。 APPLICATION_MODAL 样式允许实例阻止输入到显示中的每个其他shell。 SYSTEM_MODAL 样式允许实例阻止对所有shell的输入,包括属于不同应用程序的shell。

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 也足够了。

Even SWT.PRIMARY_MODAL would suffice in this case.

更新

另一方面,如果您不希望父母在点击孩子时失去焦点,只需禁用子项:

If on the other hand you don't want the parent to loose focus when the child is clicked, just disable the child:

shell2.setEnabled(false);

这篇关于如何在不激活shell的情况下打开shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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