pregressBar in preloader does not update

2019-09-09 19:47发布

问题:

i have this in my preloader:

import javafx.application.Preloader;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class SplashScreenView extends Preloader {

    ProgressBar bar;
    Stage stage;
    // boolean noLoadingProgress = true;

    public Scene createPreloaderScene()
    {
        bar = new ProgressBar();
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(bar);
        return new Scene(borderPane, 500, 150);
    }

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

    }

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

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

    @Override
    public void handleApplicationNotification(PreloaderNotification arg0)
    {
        if (arg0 instanceof ProgressNotification)
        {
            ProgressNotification pn = (ProgressNotification) arg0;
            bar.setProgress(pn.getProgress());
            System.out.println("Progress " + bar.getProgress());
        }
    }

}

and this in my main -Application.

    init (){
    //loading images
}

    public static void main(String[] args){
            LauncherImpl.launchApplication(Main.class, SplashScreenView.class, args);

        }

but the progressBar does not laod. instead, it is first 0.0 and then 1.0 , so the pregressBar is already full. but I have to wait for some seconds. after these some seconds my main-Application starts...

please help :(

回答1:

Same problem here. Check to notify the preloader in application from init() method and not from start() method.

@Override
public void init() throws Exception {
    // do some heavy stuff
    notifyPreloader(new ProgressNotification(0.5));
    // do some heavy stuff
    notifyPreloader(new ProgressNotification(0.8));

}

It works for me. Try it.