Javafx concurrency and gui-update

47 views Asked by At

What I'm trying to do is the following: I want to fade in an overlay screen before doing some background work and fade the overlay screen out when the task finishes.

Anyhow, java performs the background task requestService.doRequest and afterwards it fades the screen in and out. So the current order is like:

  • task
  • fade in
  • fade out

while it should be:

  • fade in
  • task
  • fade out

I think I tried every possible configuration of Threads, Tasks, Service, etc..

So how do one correctly update the gui like I try to achieve?

Controller.java:

public void doRequest(Request r) {
    Task<Float> t = new Task<Float>() {

        @Override
        protected Float call() throws Exception {
            try {
                updateProgress(.5f, 1);
                requestService.doRequest(r);
            } catch(Exception e) {
                badge.setVisible(true);
                badge.setText(e.getMessage());
            } finally {
                updateProgress(0f, 1);
            }
            return null;
        }
    };

    overlay.opacityProperty().bind(t.progressProperty());
    overlay.opacityProperty().addListener((obs, o, n) -> System.out.println(n));
    t.run();

}

Btw: If I watch the update of the overlays opacityProperty, one can see that the property gets updated in the right order, but the gui refresh seems to hang.

0

There are 0 answers