Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), ev -> {
//what you want to do
}));
timeline.setCycleCount(1);//do it x times
timeline.setCycleCount(Animation.INDEFINITE);//or indefinitely
//play:
timeline.play();
//pause:
timeline.pause();
//stop:
timeline.stop();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.print("I would be called every 2 seconds");
}
}, 0, 2000);