JavaFX预加载器不更新进度

Geo*_*iev 4 javafx

我在使用JavaFX Preloader时遇到了麻烦.在启动阶段,应用程序必须连接到数据库并读取很多,所以我认为在此期间显示启动画面会很好.问题是ProgressBar自动进入100%,我不明白为什么.

应用类.线程休眠将在以后用实际代码替换(DB连接等)

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));
}
Run Code Online (Sandbox Code Playgroud)

预载

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());
}   
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我得到以下输出:

进展0.0进展1.0

小智 7

我有同样的问题,经过两个小时的搜索和5分钟仔细阅读JavaDoc后我找到了解决方案.:)

通过notifyPreloader()方法发送的通知只能通过Preloader.handleApplicationNotification()方法处理,并且您发送的通知类型无关紧要.

所以改变你的代码如下:

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());
          }
    }
}
Run Code Online (Sandbox Code Playgroud)