Waiting for window close

Hi,
I have method, which must return result from ConfirmDialog or other similar component. How I can wait for ConfirmDialog will be closed? Callbacks are not applicable, because the result must be returned in the same method.

You cannot have in-methods waits, you have to use callback methods. In other words, you need to refactor your code in such a way, that callbacks are possible.

Hi,

I ran into exact the same problem. I found out that there is a way to solve this problem without callbacks.

I’ll try to explain with pseudo code (No ExceptionHandling // application is an extended com.vaadin.ui.Application)


Button btn = new Button("Open Dialog and Wait");
btn.addListener(new ActionListener(){
    public void actionPerformed(Event e){
    Thread t = new Thread(new Runnable(){
        public void run(){
          createAndShowDialog(); //create and add dialog to mainwindow
        }
    }

    t.start();
    //Sleep until Dialog is shown AND! added to the MainWindow. Repaint request will be generated automatically by vaadin...
    while(application.isWaitingForRepaint()){
         Thread.sleep(100);
     }
    
 

   }
}

[EDIT]

Of course that wasn’t all.First I was waiting for 100ms, no while loop. But 100 ms might not be long enough, so I changed the whole logic again and added a boolean that locks the Main Thread until the Dialog is really shown. I’ll try to post this solution completly, since I guess it’s quite interesting that waiting for a dialog to be closed in the UI-Script Logic is now no problem at all!

Here is the important part of createAndShowDialog():


dialogClosed = false;
[...]

myDialog.addListener(new CloseListener(){
      public void windowClose(Event e){
           dialogClosed = true;
      }

}

mainWindow.addWindow(myDialog);

//will not block the main-UI thread, since this method is called from another Thread
application.setIsWaitingForRepaint(false); // Causes the Main Thread to carry on. Repaints automatically now...
while(!dialogIsClosed){
    Thread.sleep(100);
}

Seems to work very smoothly now… :grin: