title: how to change scene based on time in javafx
body: <p>New to java and JavaFX so please bear with me</p>
  
  <p>I need to do a presentation of 5 3d fruit models that show in a continuous loop, 15 seconds apart: fruit1 for 15 seconds then fruit2 for 15 seconds ad so on and so forth.. until fruit5 for 15 seconds and then back to fruit1 and continues until I hit the ESC key which should close the window.</p>
  
  <p>I also understand that it's ideal to change the root group object that makes up the scene instead of changing the scene, so I changed that in my code</p>
  
  <p>I understand that a timeline is needed in order to change something in the scene as it plays out, but I've tried with something similar to what this <a href="https://stackoverflow.com/questions/34849232/how-to-change-images-with-timeline-in-javafx">answer</a> says but I don't get the logic of how to switch the root of the scene every 15seconds </p>
  
  <p>UPDATE:</p>
  
  <p>I gave up on the timeline option and I found the platform.run option as seen on this <a href="https://riptutorial.com/javafx/example/7291/updating-the-ui-using-platform-runlater" rel="nofollow noreferrer">article</a> which seems to work as I see the window updates iterating from the first fruit in the scenes array to second one but I'm not sure why it only runs once when I need it to run every 15 seconds which means that my scene switcher: nextSceneIndex() should go back and forth between 1 and 0.</p>
  
  <p>UPDATE2:</p>
  
  <p>I went back to the timeline suggestion and I implemented Sedrick's solution and it worked... I can't be happier :)</p>
  
  <p>Here's my working code!</p>
  
  <pre><code>
    public void start(Stage stage) throws Exception {
  
          BorderPane[] scenes = new BorderPane[]{createSceneApple(),createSceneCarrot(),createSceneTomato()};
  
  
          Timeline tl = new Timeline();
          tl.setCycleCount(Animation.INDEFINITE);
          KeyFrame kf_fruit = new KeyFrame(Duration.seconds(10),
                  new EventHandler&lt;ActionEvent&gt;() {
                      public void handle(ActionEvent event) {
  
                         if (index==0){
                             root.getChildren().setAll(scenes[0]);
                             index = 1;
                         }else if(index==1){
                             root.getChildren().setAll(scenes[1]);
                             index = 2;
                         }else if(index==2){
                             root.getChildren().setAll(scenes[2]);
                             index = 0;
                         }
                      }  
          });
  
          tl.getKeyFrames().add(kf_fruit);
          tl.play();
  
          Scene scene = new Scene(root, windowWidth, windowHeight);
          stage.setScene(scene);
          stage.show();
  
  }
  
  
  </code></pre>