2013-03-19 28 views
5

マルチスレッド環境でProgressBarを更新する方法を理解しようとしています。私はここで何か間違っているが、私はそれが何かを見ていない。これは3秒ごとにバーを埋めるだけですが、それはありません:JavaFx ProgressBarが更新されない

  Task<Void> task = new Task<Void>(){ 
       @Override 
       public Void call(){ 
        for (int i = 1; i < 10; i++) { 
         try { 
          Thread.sleep(3000); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         System.out.println(i); 
         updateProgress(i , 10); 
        } 
       return null;     
       } 
      }; 

      updProg = new ProgressBar(); 
      updProg.progressProperty().bind(task.progressProperty()); 

      Thread th = new Thread(task); 
      th.setDaemon(true); 
      th.start(); 

私は何が欠けていますか?

答えて

8

サンプルがうまく動作します。

サンプルが3秒ごとに少しずつ満たされ、バーが半分で完全に満たされます。

私はそれを実行可能にするためにいくつかの足場のコードに包んで、それを変更せずに動かしました(java7u15、win7)。

simpletaskprogress

import javafx.application.Application; 
import javafx.concurrent.Task; 
import javafx.scene.*; 
import javafx.scene.control.*; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class ProgressTest extends Application { 
    public static void main(String[] args) { Application.launch(args); } 
    @Override public void start(Stage stage) { 
    Task<Void> task = new Task<Void>() { 
     @Override public Void call() { 
     for (int i = 0; i < 10; i++) { 
      try { 
      Thread.sleep(100); 
      } catch (InterruptedException e) { 
      Thread.interrupted(); 
      break; 
      } 
      System.out.println(i + 1); 
      updateProgress(i + 1, 10); 
     } 
     return null; 
     } 
    }; 

    ProgressBar updProg = new ProgressBar(); 
    updProg.progressProperty().bind(task.progressProperty()); 

    Thread th = new Thread(task); 
    th.setDaemon(true); 
    th.start(); 

    StackPane layout = new StackPane(); 
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;"); 
    layout.getChildren().add(updProg); 

    stage.setScene(new Scene(layout)); 
    stage.show(); 
    } 
} 

おそらく、あなたは、ProgressBarの更新を中心に、それのバグを持っていたのJava 8の一部の早期アクセス版(現在は固定)を使用してきました。

RT-29018 ProgressBar and ProgressIndicator disappear when progressProperty is updated

+0

私はあなたのコードをコピーして、それだけで完璧に動作します。私は何とかそれを誤って統合しましたか?私はそれをサブクラスで使用し、静的な方法でプログレスバーにアクセスする必要があります。これは何らかのトラブルを引き起こす可能性があります私は7v17 btwを使用して – Chromos

+0

編集:guiはfxmlシートで設計されています。 – Chromos

+0

パッチはすでに@jewelseaで入手できますか? –

1

あなたは、最新のJDK 8の早期アクセスを使用していますか?もしそうなら、私が提出したこのバグレポートを見てください:http://javafx-jira.kenai.com/browse/RT-29018

基本的に、最近のリリースの初期アクセスビルドでは、彼らはスキンとCSSにいくつかの変更を加えました。これにより、隠れたバグが明らかになりました。親ノードよりも汚れていても、同じパルスで再描画が必要な子ノードでは、親のダーティレベルが子ノードのダーティレベルを上書きしてしまいます。

これにより、進捗状況が表示されなくなり、実際にはprogressBarが完全に見えなくなりました。ただちにupdateProgressがタスクから呼び出されました。彼らはその場所にパッチを持っている、私はこれがいつ進むのか分からない。

回避策、いずれかのパッチを待っている間にJDK7使用するか、またはあなたは私がやったんや、あなたのCSSスタイルシートの中に古いCSSからこれを適用することができます。

/*hack to get progress bar working. From: JDK7u17 jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.css */ 

/******************************************************************************* 
*                    * 
* ProgressBar                 * 
*                    * 
******************************************************************************/ 

.progress-bar { 
    -fx-skin: "com.sun.javafx.scene.control.skin.ProgressBarSkin"; 
    -fx-background-color: 
     -fx-box-border, 
     linear-gradient(to bottom, derive(-fx-color,30%) 5%, derive(-fx-color,-17%)); 
    -fx-background-insets: 0, 1; 
    -fx-indeterminate-bar-length: 60; 
    -fx-indeterminate-bar-escape: true; 
    -fx-indeterminate-bar-flip: true; 
    -fx-indeterminate-bar-animation-time: 2; 
    -fx-focus-traversable: true; 
} 

.progress-bar .bar { 
    -fx-background-color: 
     -fx-box-border, 
     linear-gradient(to bottom, derive(-fx-accent,95%), derive(-fx-accent,10%)), 
     linear-gradient(to bottom, derive(-fx-accent,38%), -fx-accent); 
    -fx-background-insets: 0, 1, 2; 
    -fx-padding: 0.416667em; /* 5 */ 
} 

.progress-bar:indeterminate .bar { 
    -fx-background-color: linear-gradient(to left, transparent, -fx-accent); 
} 

.progress-bar .track { 
    -fx-background-color: 
     -fx-box-border, 
     linear-gradient(to bottom, derive(-fx-color,-15%), derive(-fx-color,2.2%) 20%, derive(-fx-color,60%)); 
    -fx-background-insets: 0, 1; 
} 

.progress-bar:disabled { 
    -fx-opacity: 1.0 
} 
+0

thxです。事は実際にはjre 7v17を使っています。 win7とubuntuでテストしても動作しませんでした。しかし、ジュエルズの答えの正確な例はうまくいきました。私はProgressBarを実装する方法を何か持っている必要があります – Chromos

0

あなたがupdProgを定義した場合FXMLファイルの場合、ここでの初期化が問題になる可能性があります。

はちょうどこの行を削除しよう:

updProg = new ProgressBar(); 
+0

なぜdownvoteですか? – Chaiavi

+0

誰かがdownvote場合は、ポスターについては、彼の答えを修正することができますupvote得ることができるコメントを残してください。 –

0
Timeline updateProgressBar = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() { 
       @Override 
       public void handle(ActionEvent event) { 
         /*Where Seconds and TotalSeconds are counter and total 
         respectively, also put somewhere Seconds++ or 
         however you want to update progress. This time line 
         will reapit each second */ 
         progressBar.setProgress(Seconds/TotalSeconds); 
       } 
    })); 
    updateProgressBar.setCycleCount(Timeline.INDEFINITE); 
    updateProgressBar.play(); 
関連する問題