What I am trying:
I try to build a countdown timer for home workout, which runs down the same interval twice and then gives you an extra interval break. After that it should start over again with the 3 intervals.
How far did I get?:
Currently I am successfully running the first interval from thirty seconds down to zero. My problem is, that I can not determine whether the JavaFX Task is completed or not. More precisely I cannot start over with it again WITHOUT creating several self overwriting process (e.g. with a for loop).
Code and GUI:
This is my Controller.class for handling my FXML file:
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;
public class Controller {
private int time;
@FXML
private Label counterLabel;
private static StringProperty displayTime = new SimpleStringProperty("30");
@FXML
private Button startstop;
@FXML
private ProgressIndicator progress;
private static DoubleProperty prog = new SimpleDoubleProperty(0.0);
@FXML
private Slider Pausendauer; // pause length
@FXML
private Slider Trainingsdauer; //interval length
@FXML
private Slider Repeats;
public void initialize() {
progress.setProgress(0);
counterLabel.textProperty().bind(displayTime);
progress.progressProperty().bind(prog);
}
public void click(ActionEvent e) throws InterruptedException {
//event for clicking start button
//Task for Counting (Should be repeatable after running through
time = (int)Trainingsdauer.getValue();
Task<String> count = new TaskTimer(time);
displayTime.bind(count.valueProperty());
prog.bind(count.progressProperty());
count.run();
}
}
And that is my Task (Timer) class:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.concurrent.Task;
public class TaskTimer extends Task<String>{
private int time;
double fraction;
double prog;
public static void main(String[] args) {
}
public TaskTimer(int sec) {
this.time = sec;
this.fraction = 1.0 / time; //claculating for closng circle
this.prog = 0.0;
}
protected String call() throws Exception {
// TODO Auto-generated method stub
//runs scheduled without delay
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable(){
public void run() {
time = time - 1; //Decrement of time
prog = prog + fraction; //increase progress
updateProgress(prog, 1);
if(time >= 10) { // Makes 01 from 1 and so on
updateValue(time+"");
}else {
updateValue("0"+time);
}
System.out.println(time+"");
if (time <= 0) {
updateProgress(1, 1);
scheduler.shutdown();
}
}
}, 1, 1, TimeUnit.SECONDS);
return time+"";
}
}
And that's the application at work (the sad one it iteration it's capable of):
GUI Design to describe function

What can I try next? Have tried a whole bunch of things right now. Most of my "solutions" ended up with self overwriting twins and or a freezing UI.