I want to implement a timer in JavaFX and have come across this piece of code but I don't know where to put it inside my project files. Also is this the correct way to implement a timer? I want the timer to run whenever the scene is changed. So do I put it inside the initialize method of the Initializable interface or somewhere else?
final int[] secondsPassed = {0};
Timer myTimer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
secondsPassed[0]++;
Platform.runLater(() -> timerLabel.setText(String.valueOf(secondsPassed[0])));
;
}
};
myTimer.scheduleAtFixedRate(task,1000,1000);
Also why is the variable secondsPassed a final int array?