如何检测到SWT对话框已打开并且可见? [英] How do I detect that a SWT dialog has been opened and is visible?

查看:49
本文介绍了如何检测到SWT对话框已打开并且可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有许多页面的SWT WizardDialog .首次打开此对话框时,我必须检查一些条件,如果满足这些条件,则需要在新打开的对话框上显示一个弹出窗口.

I have an SWT WizardDialog with a number of pages. When this dialog first opens I have to do a check for some conditions and if those conditions are met I need to show a popup over the freshly opened dialog.

所以我有这段代码来监听 SWT.Show 事件.事件侦听器响应 SWT.Show 进行测试并显示消息框:

So I have this code to listen for SWT.Show event. The event listener responds to SWT.Show to conduct its tests and show a message box:

  final WizardDialog dialog = new WizardDialog(shell, wizard);
  dialog.setTitle("New Wizard");
  dialog.create();
  dialog.getShell().addListener(SWT.Show, new Listener()
  {
     private boolean firstShowing = true;

     @Override
     public void handleEvent(Event event)
     {
        if (firstShowing && someConditionExists())
        {
          MessageBox messageBox = new MessageBox(dialog.getShell(), SWT.OK
                 | SWT.ICON_WARNING);
          messageBox.setMessage("Test");
          messageBox.open();
          firstShowing = false;
        }
     }
  });
  dialog.open();

除非被叫得太早!调用处理程序时,该对话框不可见.我的消息框出现在对话框可见之前,并且仅当我关闭消息框时对话框才会出现.

Except it's called too soon! The dialog is not visible when the handler is called. My message box appears before the dialog is visible and the dialog only appears when I dismiss the message box.

显然, SWT.Show 是不可靠的,至少在运行它的 Window 上是不可靠的.我还尝试在激活时将此代码放入 ShellListener 中,但这甚至在上述 SWT.Show 示例之前发生.

So clearly the SWT.Show is unreliable, at least on Windows where I'm running it. I've also tried putting this code into a ShellListener on the activation but that happens even before the SWT.Show example above.

那么当对话框可见时,如何可靠地显示消息框?

So how do I reliably show a message box when the dialog is made visible?

计划B是一个基于肮脏的计时器的黑客程序,其中的计时器设置为在未来200毫秒内触发,希望该对话框在可见对话框时触发,但是很明显,这可能会引发其自身的问题.

Plan B is a dirty timer based hack where a timer is set to fire 200 ms into the future and hope that it triggers when the dialog is visible but obviously this could introduce it's own issues.

推荐答案

我在类似情况下使用(需要在可见应用程序窗口后调用 appStarted()),如下所示./p>

I'm using in similar situation (need that appStarted() is called after application window is visible) something like below.

public class App extends ApplicationWindow {

    @Override
    protected Control createContents(Composite parent) {
        // ...

        getShell().addShellListener(new ShellAdapter() {

            @Override
            public void shellActivated(ShellEvent shellevent) {
                if (!started) {
                    Shell s = (Shell) shellevent.getSource();
                    s.setVisible(true);
                    appStarted();
                    started = true;
                }
            }
        });
    }
}

也许您可以使用如下所示的内容:

Maybe You can use the same like below:

final WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setTitle("New Wizard");
dialog.create();
dialog.getShell().addShellListener(new ShellAdapter() {

    @Override
    public void shellActivated(ShellEvent shellevent) {

        if (firstShowing && someConditionExists()) {
            Shell s = (Shell) shellevent.getSource();
            s.setVisible(true);

            MessageBox messageBox = new MessageBox(dialog.getShell(), SWT.OK | SWT.ICON_WARNING);
            messageBox.setMessage("Test");
            messageBox.open();
            firstShowing = false;
        }

    }
});
dialog.open();

这篇关于如何检测到SWT对话框已打开并且可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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