title: Java count down timer using javaFX
body: <p>I'm in the middel of making a countdown timer in javafx. I've ran intro a problem: I want to be able to pause and resume the timer on a button click. </p>
  
  <p>But, when I do this, as the code is now, the timeline is not resuming at the same rate as before. </p>
  
  <p>So how do I make it so I can pause and resume the timer on a button click ? </p>
  
  <p>CODE: </p>
  
  <pre><code>package gameStats.controllers;
  
  import gameStats.Main;
  import javafx.animation.KeyFrame;
  import javafx.animation.Timeline;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.*;
  import javafx.scene.layout.AnchorPane;
  import javafx.scene.layout.Background;
  import javafx.scene.layout.BorderPane;
  import javafx.scene.paint.Color;
  import javafx.stage.Stage;
  import javafx.util.Duration;
  
  import java.io.IOException;
  import java.security.Timestamp;
  import java.sql.Time;
  import java.util.concurrent.TimeUnit;
  
  public class MainController {
  
      private Timeline timeline = new Timeline();
      @FXML
      private Label timerText;
      @FXML
      private Button startTimerButton, stopTimerButton, resetTimerButton, addTeamButton, addPointButton, removePointButton, newTimeButton;
      @FXML
      private TextField teamNameTextField;
      @FXML
      private TableView teamView;
  
      private int min;
      private int startTimeSec, startTimeMin;
      private Parent borderPane;
      public BorderPane timeBorderPane;
      private Duration keyframeToContinuefrom;
  
      public void startTimer() {
          if(!timeline.getStatus().equals("paused")) {
              if (!(startTimeMin &lt; 0)) {
                  timerText.setTextFill(Color.BLACK);
                  startTimeSec = 60; // Change to 60!
                  startTimeMin = min - 1;
                  timeline.setCycleCount(Timeline.INDEFINITE);
                  timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1), new EventHandler&lt;ActionEvent&gt;() {
                      @Override
                      public void handle(ActionEvent event) {
                          startTimeSec--;
                          boolean isSecondsZero = startTimeSec == 0;
                          boolean timeToChangeBackground = startTimeSec == 0 &amp;&amp; startTimeMin == 0;
  
                          if (isSecondsZero) {
                              startTimeMin--;
                              startTimeSec = 60;
                          }
                          if (timeToChangeBackground) {
                              timeline.stop();
                              startTimeMin = 0;
                              startTimeSec = 0;
                              timerText.setTextFill(Color.RED);
  
                          }
  
                          timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));
  
                      }
                  }));
                  timeline.playFromStart();
              } else {
                  Alert alert = new Alert(Alert.AlertType.INFORMATION, "You have not entered a time!");
                  alert.showAndWait();
              }
          }else {
  
          }
  
      }
  
      public void setTimer() {
          try {
              FXMLLoader loader = new FXMLLoader();
              loader.setLocation(Main.class.getResource("newTimeDialog.fxml"));
              Parent newTimeBorderPane = (BorderPane) loader.load();
              borderPane = newTimeBorderPane;
              Scene scene = new Scene(newTimeBorderPane);
              Stage primaryStage = new Stage();
              primaryStage.setScene(scene);
              primaryStage.showAndWait();
              if(!primaryStage.isShowing()){
                  min = newTimeController.getMin();
                  timerText.setText(" " + min);
              }else{
  
              }
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
      }
  
      public void stopTimer(){
          timeline.pause();
      }
  
      public void resetTimer(){
          timeline.stop();
          startTimeSec = 60;
          startTimeMin = min;
          timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));
      }
  
  }
  </code></pre>