JavaFX Timeline count down

Description

JavaFX Timeline count down

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {
   public static void main(String[] args) {
      Application.launch(args);/*from   w ww  .  j av a2  s  . c om*/
   }

   @Override
   public void start(Stage primaryStage) {
      VBox vb = new VBox();
      int refreshCountdown = 30;
      IntegerProperty countDown = new SimpleIntegerProperty(refreshCountdown);

      countDown.addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {

            System.out.println(newValue.intValue());

         }
      });
      final Timeline timeToRefresh = new Timeline();
      timeToRefresh.getKeyFrames().addAll(new KeyFrame(Duration.ZERO, new KeyValue(countDown, refreshCountdown)),
            new KeyFrame(Duration.seconds(refreshCountdown), new KeyValue(countDown, 0)));
      timeToRefresh.playFromStart();

      Scene scene = new Scene(vb);
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}



PreviousNext

Related