Hallo,
ich scheitere leider an dem Versuch einen Timer, welcher in der Konsole problemlos funktioniert, in einer GUI anzeigen zu lassen.
Das Problem ist, dass ich innerhalb des TimerTasks nicht auf mein Label, welches die Zeit anzeigen soll, zugreifen kann.
Falls die Antwort nicht sowieso klar ist, hier die Error-Message, wenn ich auf das Label zugreifen will:
Weiß jemand, wie ich das richtig mache?
Das selbe Problem habe ich auch, wenn etwas in einem anderen Thread abläuft.
ich scheitere leider an dem Versuch einen Timer, welcher in der Konsole problemlos funktioniert, in einer GUI anzeigen zu lassen.
Das Problem ist, dass ich innerhalb des TimerTasks nicht auf mein Label, welches die Zeit anzeigen soll, zugreifen kann.
Java:
package application;
import java.util.Timer;
import java.util.TimerTask;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class SampleController {
static int seconds,interval;
static Timer timer;
@FXML
private Button btnStart;
@FXML
private Label LabelTime;
@FXML
void btnStartPressed(ActionEvent event) {
countdown();
}
public void countdown() {
int seconds = 60;
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = (seconds);
System.out.println(seconds);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(setInterval());
// LabelTime.setText(""+setInterval()); Das ist das Problem
}
}, delay, period);
}
private static final int setInterval() {
if (interval == 1)
timer.cancel();
return --interval;
}
}
Code:
Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:575)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:204)
at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(LabelSkin.java:49)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:144)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:49)
at javafx.beans.property.StringProperty.setValue(StringProperty.java:65)
at javafx.scene.control.Labeled.setText(Labeled.java:145)
at application.SampleController$1.run(SampleController.java:45)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Das selbe Problem habe ich auch, wenn etwas in einem anderen Thread abläuft.