2016-11-18 8 views
2

JavaFXを使用して簡単なUIを実行しています。 SQLクエリとExcelファイルを処理し、実行中の進行状況バーウィンドウを表示するアプリケーションを作成しました。私はtakの使用を推奨する他の多くのstackoverflow質問を読んでいます。ただし、JavaFXタスクを使用している場合でも、進行状況バーは表示されますが、空のままです。ここに私のコードです:JavaFX - タスクでも進行していない未確定の進行状況バー

public class ProgressBarWindow extends VBox 
{ 
public ProgressBarWindow(Stage stage) 
{ 
    final ProgressBar pb = new ProgressBar(); 
    pb.setProgress(ProgressBar.INDETERMINATE_PROGRESS); 

    Label label = new Label("Le fichier de classement est en cours de création."); 

    setStyle(
      "-fx-padding:10; -fx-background-color: honeydew; -fx-border-color: derive(honeydew, -30%); -fx-border-width: 3;"); 

    setPadding(new Insets(20, 20, 20, 20)); 
    setAlignment(Pos.BASELINE_CENTER); 
    setSpacing(25); 

    getChildren().addAll(label); 
    getChildren().addAll(pb); 
} 
} 

// Called when the user clicks the Finish button in a wizard 
public void finish() 
{ 
    Task task = new Task<Void>() 
    { 
     @Override 
     public Void call() 
     { 
      try 
      { 
       StandingsCreationHelper.createStandingsFile(); 
      } 
      catch (Exception ex) 
      { 
       Alert alertStandingsFileExecution = Dialog.getExceptionDialog(ex); 
       alertStandingsFileExecution.showAndWait(); 
      } 
      return null; 
     } 
    }; 

    new Thread(task).start(); 

    Scene scene = new Scene(new ProgressBarWindow(owner)); 
    owner.setScene(scene); 
    owner.show(); 

    do 
    { 
     try 
     { 
      Thread.sleep(500); 
     } 
     catch (InterruptedException e) 
     { 
      // recommended because catching InterruptedException clears interrupt flag 
      Thread.currentThread().interrupt(); 
      return; 
     } 
    } 
    while (StandingsCreationHelper.isFinished() == false); 

    owner.close(); 
} 

ありがとうございました!

注:ProgressBarWindowを単独で実行する(ステージとシーンを追加して手動で表示する)場合、進行状況バーには前後の動きが表示されます。

答えて

2

FXアプリケーションスレッドをブロックして、レンダリングを妨げています。決してこれをしないでください。

タスクが完了するとコードを実行するには、タスクのonSucceededハンドラを使用し、例外を処理するハンドラはonFailedです。どちらもFXアプリケーションスレッド上で実行されます.FXアプリケーションスレッドを使用すると、そこからUIと対話できます。

public void finish() { 

    Task task = new Task<Void>() { 

     @Override 
     public Void call() throws Exception { 
      StandingsCreationHelper.createStandingsFile(); 
      return null; 
     } 
    }; 

    task.setOnFailed(e -> { 
     Throwable ex = task.getException(); 
     Alert alertStandingsFileExecution = Dialog.getExceptionDialog(ex); 
     alertStandingsFileExecution.showAndWait(); 
    }); 

    task.setOnSucceeded(e -> owner.close()); 

    Scene scene = new Scene(new ProgressBarWindow(owner)); 
    owner.setScene(scene); 
    owner.show(); 

    new Thread(task).start(); 

} 
+0

あなたのコードははるかにクリーンで、私の問題を解決します:)!詳細は、setOnFailedコマンドでキャスト例外を追加する必要がありました。Exception ex =(Exception)task.getException();どうもありがとうございました! –

+0

ああ、はい、 'getException()'は 'Throwable'を返します。更新された答え( 'getExceptionDialog(...)'メソッドに渡すためにまだキャストが必要な場合があります)。 –

+0

まだキャストが必要です:)。私は元の投稿で、どのようにFXスレッドをブロックしていたのだろうと思っていましたか?私はタスクオブジェクトがそれを避けるためにそこにいると思った。 –

関連する問題