title: How do you make code refresh itself at regular intervals in JavaFX?
body: <p>I am trying to create a clock program and I am new to javaFX programming, my code reads as follows:</p>
  
  <pre><code>package clock;
  
  import java.util.Calendar;
  import javafx.animation.KeyFrame;
  import javafx.animation.KeyValue;
  import javafx.animation.Timeline;
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.effect.*;
  import javafx.scene.image.Image;
  import javafx.scene.image.ImageView;
  import javafx.scene.layout.StackPane;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.Circle;
  import javafx.stage.Stage;
  import javafx.stage.StageStyle;
  import javafx.util.Duration;
   /**
   *
   * @author Jaskey     */
  public class Clock extends Application {
  
  /**
   * @param args the command line arguments
   */
  
  private ImageView secondHand;
  private ImageView minuteHand;
  private ImageView hourHand;
  private Calendar realTime = Calendar.getInstance();
  int hours = realTime.get(Calendar.HOUR_OF_DAY);
  int minutes = realTime.get(Calendar.MINUTE);
  int seconds = realTime.get(Calendar.SECOND);
  
  public static void main(String[] args) {
      launch(args);
  }
  
  
  
  @Override
  public void start(Stage primaryStage) {
      primaryStage.setTitle("Clock 0.03");
      primaryStage.initStyle(StageStyle.TRANSPARENT);
  
      Circle clock = new Circle(150,Color.DARKGRAY);
      clock.setEffect(new Glow(0.6f));
      clock.setEffect(new InnerShadow(15, Color.BEIGE));
      clock.setEffect(new DropShadow());
  
  
  
      Circle clockCentre = new Circle(7,Color.THISTLE);
      clockCentre.setEffect(new Shadow());
      clockCentre.setEffect(new BoxBlur());
  
  
  
       minuteHand = new ImageView(new Image(Clock.class.getResourceAsStream("images/minuteHand.png")));
       minuteHand.setFitHeight(370);
       minuteHand.setFitWidth(370);
       minuteHand.setOpacity(0.85f);
  
  
      secondHand = new ImageView(new Image(Clock.class.getResourceAsStream("images/secondHand.png")));
      secondHand.setFitHeight(370);
      secondHand.setFitWidth(370);
      secondHand.setOpacity(0.85f);
  
  
      hourHand = new ImageView(new Image(Clock.class.getResourceAsStream("images/hourHand.png")));
      hourHand.setFitHeight(370);
      hourHand.setFitWidth(370);
      hourHand.setOpacity(0.85f);
  
  
  
      if(seconds == seconds){
  
          secondHand.setRotate(seconds*6);
  
      }
  
              if(minutes == minutes){
  
          minuteHand.setRotate(minutes*6);
  
      }
  
           if(hours == hours){
  
          hourHand.setRotate(hours*30);
  
      }
  
  
  
  
  
  
         final Timeline timelineSeconds = new Timeline();
      timelineSeconds.setCycleCount(Timeline.INDEFINITE);
      timelineSeconds.setAutoReverse(false);
      final KeyValue SecondHand = new KeyValue(secondHand.rotateProperty(), 360);
      final KeyFrame seconds2 = new KeyFrame(Duration.seconds(60), SecondHand);
      timelineSeconds.getKeyFrames().addAll(seconds2);
      timelineSeconds.play();
  
         final Timeline timelineMinutes = new Timeline();
      timelineMinutes.setCycleCount(Timeline.INDEFINITE);
      timelineMinutes.setAutoReverse(false);
      final KeyValue MinuteHand = new KeyValue(minuteHand.rotateProperty(), 360);
      final KeyFrame minutes2 = new KeyFrame(Duration.minutes(60), MinuteHand);
      timelineMinutes.getKeyFrames().add(minutes2);
      timelineMinutes.play();
  
             final Timeline timelineHours = new Timeline();
      timelineHours.setCycleCount(Timeline.INDEFINITE);
      timelineHours.setAutoReverse(false);
      final KeyValue HourHand = new KeyValue(hourHand.rotateProperty(), 360);
      final KeyFrame hours2 = new KeyFrame(Duration.hours(12), HourHand);
      timelineHours.getKeyFrames().add(hours2);
      timelineHours.play();
  
  
  
  
  
      StackPane root = new StackPane();
  
  
      root.getChildren().addAll(clock, clockCentre, hourHand, minuteHand, secondHand);
      Scene myScene = new Scene(root, 650, 450);
      myScene.setFill(null);
      primaryStage.setScene((myScene));
      primaryStage.show();
  
  
          }
  }
  </code></pre>
  
  <p>The clock hands were working fine (just not in time with the current time), so I added this part of the code to fix the problem:</p>
  
  <pre><code>if(seconds == seconds){
  
          secondHand.setRotate(seconds*6);
  
      }
  
              if(minutes == minutes){
  
          minuteHand.setRotate(minutes*6);
  
      }
  
           if(hours == hours){
  
          hourHand.setRotate(hours*30);
  
      }
  </code></pre>
  
  <p>The only problem is every minute this causes the second hand to jolt, and every hour the minute hand and every half day for the hour hand. I know the solution to the problem lies with refreshing the code above every 59 seconds - that is where I need help - How do I make it refresh itself every 59 seconds???!</p>
  
  <p>thanks...</p>