Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX preloader not updating progress

Tags:

javafx

I'm having trouble with the JavaFX Preloader. During the start phase the application will have to connect to a DB and read many so I thought it would be nice to display a splash screen during this time. The problem is the ProgressBar automaticly goes to 100% and I don't understand why.

Application class. Thread sleep will be replaced by real code later (DB connection etc)

public void init() throws InterruptedException
{
   notifyPreloader(new Preloader.ProgressNotification(0.0));
   Thread.sleep(5000);
   notifyPreloader(new Preloader.ProgressNotification(0.1));
   Thread.sleep(5000);
   notifyPreloader(new Preloader.ProgressNotification(0.2));
}

Preloader

public class PreloaderDemo extends Preloader {

ProgressBar bar;
Stage stage;

private Scene createPreloaderScene() {
    bar = new ProgressBar();
    bar.getProgress();
    BorderPane p = new BorderPane();
    p.setCenter(bar);
    return new Scene(p, 300, 150);        
}

@Override
public void start(Stage stage) throws Exception {
    this.stage = stage;
    stage.setScene(createPreloaderScene());        
    stage.show();
}

@Override
public void handleStateChangeNotification(StateChangeNotification scn) {
    if (scn.getType() == StateChangeNotification.Type.BEFORE_START) {
        stage.hide();
    }
}

@Override
public void handleProgressNotification(ProgressNotification pn) {
    bar.setProgress(pn.getProgress());
    System.out.println("Progress " + bar.getProgress());
}   

For some reason I get the following output:

Progress 0.0 Progress 1.0

like image 512
Georgi Georgiev Avatar asked Mar 22 '23 06:03

Georgi Georgiev


1 Answers

I had same problem and I found solution after two hours of searching and 5 minutes of carefully reading of JavaDoc.:)

Notifications send by notifyPreloader() method can be handled only by Preloader.handleApplicationNotification() method and it doesn't matter which type of notification are you sending.

So change you code like this:

public class PreloaderDemo extends Preloader {

   .... everything like it was and add this ...

   @Override
   public void handleApplicationNotification(PreloaderNotification arg0) {
          if (arg0 instanceof ProgressNotification) {
             ProgressNotification pn= (ProgressNotification) arg0;
             bar.setProgress(pn.getProgress());
             System.out.println("Progress " + bar.getProgress());
          }
    }
}
like image 55
none_ Avatar answered Mar 31 '23 21:03

none_