MISTAKE! Exception on thread "AWT-EventQueue-0" java.lang.IllegalStateException: not on the FX application thread; currentThread = AWT-EventQueue-0
I'm not sure what I am doing wrong.
I've created a timer that updates the text graphics at the top of my GUI. When the timer ends, it changes the variable (which is associated with the listener) and adds a new "Scene / Group / Node" graphical object to my GUI.
-Setting my variable // Associated with ChangeListener -And adding a scene to my GUI // Not a listener, but adding new items to my GUI
Both of these additions are causing my program to crash with this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:235)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:393)
An example of an error code:
int delay = new Integer(1000); //milliseconds
time_left=time_limit;
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent arg0) {
// TODO Auto-generated method stub
timer.setText(time_left+" SECONDS LEFT");
time_left=time_left-1;
if (time_left<0)
{
//time_left=time_limit;
mytimer.stop();
//mytimer.start();
root.getChildren().get(2).setOpacity(.2);
//root.getChildren().add(3,newScene()); //This causes to crash //Adds a new Scene to my GUI
current_player.setValue(false); //Also causes to crash - current_player has a Listener on it
//System.out.println("TIMER RAN OUT!");
}
}
};
mytimer=new Timer(delay, taskPerformer);
mytimer.start();
my current_player listener:
current_player.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
//Code in Here
}
{);
source to share
Is the exception message obvious? You are using a Swing timer , which calls its listener on the event dispatch thread (required when using Swing apps).
However, you are updating your JavaFX interface on this thread, not on the FX application application.
I'm not familiar with JavaFX yet, but hopefully they have a timer mechanism like this that you can use, or something similar EventQueue.invokeAndWait
that you could use in a methodactionPerformed
Edit:
did a bit of Googling and found this page about concurrency in JavaFX which might be a good read. However, there is no mention of the timer class. Perhaps the class Animation
is what you are looking for
source to share