java.lang.IllegalStateException: 不在 FX 应用程序线程上;当前线程 = 线程 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29449297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:01:11  来源:igfitidea点击:

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

javamultithreadingjavafx

提问by Vicente Bermúdez

I'm trying to set the string of a Text object from a Thread but it's giving me this error:

我正在尝试从 Thread 设置 Text 对象的字符串,但它给了我这个错误:

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.access0(Unknown Source)
at javafx.scene.text.Text.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.run(TableController.java:70)

HandlerClass:

处理程序类:

@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.

我正在学习教程。教程中的那个人没有使用 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.

我试过使用Platform.runLater(),它确实有效,但它使我的程序崩溃。我还尝试在该Platform.runLater(new Runnable() { })方法上创建一个计时器,但它给了我与以前相同的错误。

采纳答案by ItachiUchiha

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

timer.setText()进去Platform.runLater()。在它之外,在while循环内,添加Thread.sleep(1000);

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

Illegal State Exception 背后的原因是您试图在 JavaFX 应用程序线程以外的某个线程上更新 UI。

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.

您的应用程序在添加时崩溃的原因是您通过添加要在 UI 线程上无限执行的进程来使 UI 线程过载。让线程休眠 1000 毫秒将帮助您解决这个问题。

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

如果可能,将 while(true) 替换为 Timer 或 TimerTask。

For more options follow this link

有关更多选项,请点击此链接