java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = Thread-4 [英] java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4

查看:1727
本文介绍了java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = Thread-4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

处理程序类:

@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();
}

我正在关注教程
本教程中的人没有使用JavaFX。

I'm following a tutorial. The guy in the tutorial is not using JavaFX.

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

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.

推荐答案

平台中包装 timer.setText()。 runLater()。在它之外,在while循环中,添加 Thread.sleep(1000);

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

非法状态异常背后的原因您是否正在尝试在JavaFX应用程序线程以外的某些线程上更新UI。

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

您添加它时应用程序崩溃的原因是您正在重载UI线程无限地添加要在UI线程上执行的进程。让线程休眠1000毫秒将帮助您克服这个问题。

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.

如果可能的话,用Timer或TimerTask替换while(true)。

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

有关更多选项,请参阅此链接

For more options follow this link

这篇关于java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = Thread-4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆