java - 为什么我在 JavaFX 上得到 “Not on FX application thread”?

标签 java multithreading javafx

我正在学习 JavaFX,我制作了一个演示应用程序,其中有一个标签检测值变化并改变其显示。我试图将 Label.textProperty 绑定(bind)到该值,但仍然不起作用。这是我的代码:

public class MainApp extends Application {

public static void main(String[] args) {
    launch(args);
}

private Temp aTemp = new Temp();

@Override
public void start(Stage primaryStage) {

    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml"));      
        BorderPane root = (BorderPane)loader.load();
        SampleController sampleController = loader.getController();

        Scene scene = new Scene(root,600,600);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.show();

        sampleController.setModel(aTemp);

    } catch(Exception e) {
        e.printStackTrace();
    }
}}

这是模型

public class Temp {
private StringProperty temp = new SimpleStringProperty("a");

private final ExecutorService service = Executors.newCachedThreadPool();

public Temp() {

    task.startConnection();
    service.submit(new Task<Integer>() {

        @Override
        public Integer call() {
            while(true) {
                try {
                    Thread.sleep(1000);
                }catch(Exception e) {
                    e.printStackTrace();
                }

                setTemp(getTemp() + "b");
                System.out.println(getTemp());

            }
        }
    });
}

public StringProperty getProperty() {
    return this.temp;
}

public String getTemp() {
    return this.temp.get();
}

public void setTemp(String value) {
    this.temp.set(value);
}

然后是 Controller

public class SampleController implements Initializable {

private Temp aTemp;

@FXML private Label tempLabel;


@Override
public void initialize(URL arg0, ResourceBundle arg1) {


}

public void setModel(Temp aTemp) {
    this.aTemp = aTemp;

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            tempLabel.textProperty().bind(aTemp.getProperty());
        }
    });

}}

我得到的是 Label 更改为“a”但之后不会更改并且出现此异常:

Exception in thread "pool-2-thread-1" 
java.lang.IllegalStateException: Not on FX application thread; currentThread = pool-2-thread-1

最佳答案

充实我的评论:如果您想让模型完全不了解 UI,则不要将它直接绑定(bind)到控件的属性。取而代之的是,在 UI 端有一些模型属性的监听器,这些模型属性会更改 fx 线程上的控件属性。

一段代码(未经测试,只是复制和调整 - 所以可能甚至无法编译;):

public void setModel(Temp aTemp) {
    this.aTemp = aTemp;

    aTemp.tempProperty().addListener(this::updateLabelText) 

}}

private void updateLabelText() {
   Platform.runLater(() -> label.setText(aTemp.getTemp()));  
} 

关于java - 为什么我在 JavaFX 上得到 “Not on FX application thread”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55257023/

相关文章:

java - 如何更改 javafx 中组合框项目的文本对齐方式?

JavaFX LineChart - 单击图例时在前面移动一条线

java - 在 GridPane FXML JavaFX 上添加动态按钮

JavaFX - 将 GridPane 膨胀到父级的大小

c++ - 自动线程恢复 C++

java - 如何在http请求中指定范围?

android - AsyncTask 在线程完成之前不调用 onPreExecute

Java Process.waitFor() 和 Readline 挂起

java - 在mysql数据库中导入csv

java - 在 Spring 的 Classpath 中发现 Plugin-JARS