2017-10-21 13 views
0

私は次の問題に直面しています:JavaFxでprogressBarが更新されていません。javaFXでプログレスバーが更新されない

public class FXMLDocumentController implements Initializable { 
static final int max = 1000000; 

Task task = new Task<Void>() { 
    @Override public Void call() { 

     for (int i = 1; i <= max; i++) { 
      updateProgress(i, max); 
     } 
     return null; 
    } 
}; 
@FXML 
public AnchorPane root; 
@FXML 
public ProgressBar progressWater; 
@FXML 
public ProgressBar levelGas; 



@Override 
public void initialize(URL url, ResourceBundle rb) { 
    if (!Main.isSplashLoaded) { 


     loadSplashScreen(); 

    } 

} 

private void loadSplashScreen() { 
    try { 
     Main.isSplashLoaded = true; 

     StackPane pane = FXMLLoader.load(getClass().getResource(("SplashFXML.fxml"))); 
     root.getChildren().setAll(pane); 


     FadeTransition fadeIn = new FadeTransition(Duration.seconds(3), pane); 
     fadeIn.setFromValue(0); 
     fadeIn.setToValue(1); 
     fadeIn.setCycleCount(1); 

     FadeTransition fadeOut = new FadeTransition(Duration.seconds(3), pane); 
     fadeOut.setFromValue(1); 
     fadeOut.setToValue(0); 
     fadeOut.setCycleCount(1); 

     fadeIn.play(); 

     fadeIn.setOnFinished((e) -> { 
      fadeOut.play(); 



     }); 

     fadeOut.setOnFinished((e) -> { 
        AnchorPane paneMain; 
      try { 
       paneMain = FXMLLoader.load(getClass().getResource("/sample/FXMLDocument.fxml")); 

     root.getChildren().setAll(paneMain); 
     paneMain.setOpacity(0); 

     FadeTransition fadeInmain = new FadeTransition(Duration.seconds(3), paneMain); 
     fadeInmain.setFromValue(0); 
     fadeInmain.setToValue(1); 
      fadeInmain.play(); 
      fadeInmain.setOnFinished((ex)-> { 
       // progressWater.setProgress(0.5); 
       double maxlev = progressWater.getPrefWidth(); 
       progressWater.progressProperty().bind(fadeInmain.fromValueProperty()); 
    /** 
    * Action goes here 
    */ 

       new Thread(){ 
        public void run() { 
         for (double i = 0.0; i < maxlev; i++){ 
          final double step = i; 
          Platform.runLater(() -> 

            progressWater.setProgress(step/10)); 
          System.out.printf("Complete: %02.2f%n", step /10); 

          try { 
           Thread.sleep(1000); 
          } catch(InterruptedException ex) { 
           Thread.currentThread().interrupt(); 
          } 
         } 
        } 
       }.start(); 

       progressWater.progressProperty().unbind(); 

    /** 
     * Action ends here 
     */ 
      }); 

      } catch (IOException ex) { 
       Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     }); 

    } catch (IOException ex) { 
     Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

}完了したコード、スレッドの更新を実行し、それがコンソールに印刷する場合

しかし、progressWaterバーに更新されない:

Iは、次のコードを書かれていますUI。以下 は私のFXMLドキュメントです:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.ProgressBar?> 
<?import javafx.scene.effect.GaussianBlur?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.AnchorPane?> 

    <AnchorPane id="MainPane" fx:id="root" prefHeight="322.0"  prefWidth="232.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.FXMLDocumentController"> 

    <children> 

     <ImageView fitHeight="322.0" fitWidth="232.0" pickOnBounds="true"> 
     <image> 
      <Image url="@background.jpg" /> 
     </image> 

     </ImageView> 
     <ProgressBar fx:id="progressWater" layoutX="-80.0" layoutY="170.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="5.0" prefWidth="200.0" progress="1.0" rotate="90.0"> 
      <effect> 
       <GaussianBlur radius="2.0" /> 
      </effect> 

     </ProgressBar> 
     <ProgressBar fx:id="levelGas" layoutX="113.0" layoutY="170.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="5.0" prefWidth="200.0" progress="0.0" rotate="90.0"> 
      <effect> 
       <GaussianBlur radius="2.0" /> 
      </effect> 
     </ProgressBar> 

    </children> 
</AnchorPane> 

私が間違っているのかについての任意のアイデア?

+0

タスクの開始直後に 'progressWater.progressProperty()。unbind();'が表示されます。しかし、コード全体のロジックを追跡しませんでした。あまりにも多く。以下の答えと同様に、進捗をアニメーションの価値に結び付けることは、私がかつて誰も見たことがないものです。 –

+0

これは私の論理ですが、できなければ、javafxの完全な初心者ですから、これで私を助けてくれますか?ありがとうございました! – bgilca

+0

[JavaFX並行性](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm)は、その動作を説明します。 –

答えて

0

あなたが、あなたは一見、更新されていないことを全く別のものに進捗プロパティを結合している

   progressWater.progressProperty().bind(fadeInmain.fromValueProperty()); 

..あなたが進行を回転し、更新スレッドを実行する前に、あることがすぐに私に起こりますこのバインディングを削除してプロパティをフロートさせ、fadeinプロパティから伝播するイベントに結びつけないようにしましたか?

+0

それを試しましたが、運がない、それはまだ更新されません。 – bgilca

関連する問題