Toda vez que clico no botão, na tentativa de iniciar o cronometro, esse erro acontece:
Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
referindo-se a essa linha de código : `
lblMainChronometer.setText(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, secondsHundredth));
Este é o FXMLDcumentControler do meu programa :
package chronometer;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
@FXML
private Label lblMainChronometer;
private static int startTime;
private void startCounter() { startTime = (int) System.currentTimeMillis(); }
private static int currentTimeHundredth() {
double currentTimeMillis;
currentTimeMillis = startTime - System.currentTimeMillis();
int currentTimeHundreth = (int) Math.floor(currentTimeMillis / 10);
return currentTimeHundreth;
}
private Runnable mainChronometer = new Runnable() {
@Override
public void run() {
while (true) {
int secondsHundredth = currentTimeHundredth() % 100;
int currentTimeSeconds = currentTimeHundredth() - secondsHundredth;
int seconds = (int) (currentTimeSeconds % 60);
int minutes = (int) (currentTimeSeconds / 60);
minutes %= 60; // to stay just the minutes (excluding the hours)
int hours = (int) (minutes / 60);
lblMainChronometer.setText(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, secondsHundredth));
}
}
};
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {
startCounter();
new Thread(mainChronometer).start();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Me ajudem, por favor!!!