This can be achieved in various ways.
PauseTransition is one of many suitable solutions. It waits for X time interval , and then runs Task . It can start , restart , stop at any moment .
Here is an example of how it can be used to achieve a similar result.

Full code
import javafx.animation.PauseTransition; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.util.Duration; import java.util.stream.IntStream; public class Main extends Application { int questionIndex = 0; int noOfQuestions = 10; @Override public void start(Stage stage) { VBox box = new VBox(10); box.setPadding(new Insets(10)); Scene scene = new Scene(new ScrollPane(box), 500, 200); ObservableList<String> questions = FXCollections.observableArrayList("1) Whats your (full) name?", "2) How old are you?", "3) Whats your Birthday?", "4) What starsign does that make it?", "5) Whats your favourite colour?", "6) Whats your lucky number?", "7) Do you have any pets?", "8) Where are you from?", "9) How tall are you?", "10) What shoe size are you?"); ObservableList<String> answers = FXCollections.observableArrayList(); final PauseTransition pt = new PauseTransition(Duration.millis(5000)); Label questionLabel = new Label(questions.get(questionIndex)); Label timerLabel = new Label("Time Remaining : "); Label time = new Label(); time.setStyle("-fx-text-fill: RED"); TextField answerField = new TextField(); Button nextQuestion = new Button("Next"); pt.currentTimeProperty().addListener(new ChangeListener<Duration>() { @Override public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) { time.setText(String.valueOf(5 - (int)newValue.toSeconds())); } }); box.getChildren().addAll(questionLabel, answerField, new HBox(timerLabel, time), nextQuestion); nextQuestion.setOnAction( (ActionEvent event) -> { answers.add(questionIndex, answerField.getText());
source share