简体   繁体   中英

java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4

I'm trying to set the string of a Text object from a Thread but it's giving 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'm following a tutorial . The guy in the tutorial is not using JavaFX.

I have tried using Platform.runLater() , it does work but it crashes my program. I also tried creating a Timer on the Platform.runLater(new Runnable() { }) method but it gives me the same error as before.

Wrap timer.setText() in Platform.runLater() . Outside it, inside the while loop, add Thread.sleep(1000);

The reason behind Illegal State Exception is you are trying to update UI on some thread other than JavaFX Application thread.

The reason why your app was crashing when you added it was you were overloading the UI thread by adding a process to be executed on the UI thread infinitely. Making the thread sleep for 1000ms will help you overcome the issue.

If possible replace while(true) with a Timer or TimerTask.

For more options follow this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM