0

I'm trying to show a timer to the nearest 0.1 seconds in a TextField I've created in a JavaFX GUI using Scene Builder 2. The timer is working fine but I can't get it to show the actual countdown as it is in progress in the TextField, it will just change from the initial setting to zero after the count has finished. I have also tried using a label as some forum answers seemed to suggest that will work but it displays the same behaviour. If i try to set the TextField from with the Thread loop I get a StackOverflow error.

Timer Class

    package extrusion;

public class CalibrationTimer {

//  Controller controller = new controller();
    int initialTimerSetting;
    int count;

    public void startTimer() throws InterruptedException {

        for (int count = (initialTimerSetting*10); count >=0; count--){
        Thread.sleep(100);
//      controller.timeSecs.setText(Integer.toString(count)); this invokes a StackOverflow error
        }
    }

    public void setInitialTimer(int setInitialTimer) {
        initialTimerSetting = setInitialTimer;
    }

    public double getInitialTimer() {
        return initialTimerSetting;
    }

    public int getCount() {
        return count;
    }
}

Form Controller

    package extrusion;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;

public class Controller implements Initializable{

        CalibrationTimer calTimer = new CalibrationTimer();

        @FXML
        public TextField timeSecs;

        @Override
        public void initialize(URL location, ResourceBundle resources) {
            timeSecs.setText(Double.toString(0.0));

        }

        @FXML
        public void calibrationStartButtonAction(ActionEvent event) throws InterruptedException{
            calTimer.setInitialTimer(Integer.parseInt(timeSecs.getText()));
            calTimer.startTimer();
            timeSecs.setText(Integer.toString(calTimer.getCount()));  //does not count down but returns zero after the count has finished
        }

}
I Campbell
  • 11
  • 3

0 Answers0