Java – how to detect whether the SWT dialog box is open and visible?

I have a SWT wizarddialog with many pages When this dialog box opens for the first time, I have to check some conditions. If these conditions are met, I need to display a pop-up window on the newly opened dialog box

So I have this code to listen to SWT Show event Event listener response SWT Show tests and displays 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())
        {
          Message@R_827_2419@ message@R_827_2419@ = new Message@R_827_2419@(dialog.getShell(),SWT.OK
                 | SWT.ICON_WARNING);
          message@R_827_2419@.setMessage("Test");
          message@R_827_2419@.open();
          firstShowing = false;
        }
     }
  });
  dialog.open();

Except it's too fast! This dialog box is not visible when the handler is called My message box appears before the dialog box is visible. The dialog box appears only when I close the message box

Obviously SWT Show is unreliable, at least on windows where I run it I also try to put this code into the shelllistener when activated, but this is even the case in SWT The show example has happened before

How to reliably display the message box when the dialog box is visible?

Plan B is a hack based on dirty timers. One of the timers is set to trigger in the next 200ms, and it is hoped that it will trigger when the dialog box is visible, but obviously this may introduce its own problems

Solution

I use it in a similar case (I need to call appStarted () after I see it in the application window) as shown 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 it like this:

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);

            Message@R_827_2419@ message@R_827_2419@ = new Message@R_827_2419@(dialog.getShell(),SWT.OK | SWT.ICON_WARNING);
            message@R_827_2419@.setMessage("Test");
            message@R_827_2419@.open();
            firstShowing = false;
        }

    }
});
dialog.open();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>