Vaadin view doesn't change until after a method call

Hello everyone,

Please forgive me if this is a silly question, but I’m new to web application development with vaadin.

Here is my problem:

I have a start view which asks the user for some input and there is a proceed button to go to the next step of my application, which is another view. Depending on the user input, my application should display one or the other view and after that, run a method that executes some computations.
I use a navigator to navigate between the views and I have included the above methods (the ones that execute the computations) in a ViewChangeListener to execute after the view change.

The problem is that, when I click the proceed button of the first view, the view doesn’t change until after the exit of the computation method.

I quore a part of my code, if that helps anyone:

navigator.addViewChangeListener(new InputToDiagChange());

if ([i]
condition
[/i]) {
navigator.addView("inference", diag.getInferenceView());
navigator.navigateTo("inference");
}
else {
  navigator.addView("diagnosis", diag.getDiagnosisView());
  navigator.navigateTo("diagnosis");
}

public class InputToDiagChange implements ViewChangeListener{
    @Override
    public boolean beforeViewChange(ViewChangeEvent event) {
        return true;
    }

    @Override
    public void afterViewChange(ViewChangeEvent event) {
        if(event.getOldView().getClass().equals(InputView.class) && 
                event.getNewView().getClass().equals(InferenceView.class)){
            diag.computeInferrence(); //This is a method that executes computations
        }            
        if(event.getOldView().getClass().equals(InputView.class)
                && event.getNewView().getClass().equals(DiagnosisView.class){
            diag.runDiagnosis(); //This is a method that executes computations
        }            
    }
}

Does anyone have a clue? Am I doing something wrong here?

Remember that Vaadin is a Web framework, normally using HTTP request/responses to communicate between the server and the client. The afterViewChange event is fired after the logical view change on the server is done, but the information about the view change cannot be sent to the browser before the request that triggered the change has finished executing. Thus, if you block the request handling thread by doing some heavy computation synchronously, the client isn’t notified until the computation is done.

You’ll probably need to move the computation part to a separate worker thread that can execute independently of request processing. Note, though, that if after the computation you need to again update the UI, it will be a bit tricky. In basic HTTP, nothing can be sent to the client unless the client requests it, so you’ll either need to poll the server (in Vaadin 7.1, you can use the UI.setPollInterval method) or use a server push functionality (also included in Vaadin 7.1). Also remember that whenever you want to access a Vaadin UI asynchronously, from outside regular request processing, you need to lock the VaadinSession – this is usually easiest using one of the UI.access and VaadinSession.access methods.