I am trying to set the string of a Text object from a stream, but this gives me this error:
Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source) at javafx.scene.Scene.addToDirtyList(Unknown Source) at javafx.scene.Node.addToSceneDirtyList(Unknown Source) at javafx.scene.Node.impl_markDirty(Unknown Source) at javafx.scene.shape.Shape.impl_markDirty(Unknown Source) at javafx.scene.Node.impl_geomChanged(Unknown Source) at javafx.scene.text.Text.impl_geomChanged(Unknown Source) at javafx.scene.text.Text.needsTextLayout(Unknown Source) at javafx.scene.text.Text.needsFullTextLayout(Unknown Source) at javafx.scene.text.Text.access$200(Unknown Source) at javafx.scene.text.Text$2.invalidated(Unknown Source) at javafx.beans.property.StringPropertyBase.markInvalid(Unknown Source) at javafx.beans.property.StringPropertyBase.set(Unknown Source) at javafx.beans.property.StringPropertyBase.set(Unknown Source) at javafx.scene.text.Text.setText(Unknown Source) at uy.com.vincent.fx.handling.TableController$1.run(TableController.java:70)
Handler Class:
@FXML private Text timer; @Override public void initialize(URL url, ResourceBundle rb) { init(); new Thread() { public void run() { while(true) { Calendar cal = new GregorianCalendar(); int hour = cal.get(cal.HOUR); int minute = cal.get(cal.MINUTE); int second = cal.get(cal.SECOND); int AM_PM = cal.get(cal.AM_PM); String time = hour + "" + minute + "" + second; timer.setText(time); } } }.start(); }
I follow the tutorial . The guy in the tutorial does not use JavaFX.
I tried using Platform.runLater() , it works, but it crashes my program. I also tried to create a timer using the Platform.runLater(new Runnable() { }) method, but it gives me the same error as before.
java multithreading javafx
Vicente bermúdez
source share