简体   繁体   English

JAVA FX - 如何从窗格中删除 object?

[英]JAVA FX - How can I remove an object from a pane?

I'm trying to add a project with java fx.我正在尝试使用 java fx 添加一个项目。 On this project I have a stack pane containing an AnchorPane with all the controls, and an empty VBox on top.在这个项目中,我有一个堆栈窗格,其中包含一个带有所有控件的 AnchorPane,顶部有一个空的 VBox。

When I perform call to a remote API, which will take a while to respond, I load a ProgressIndicator on the VBox.当我调用远程 API 时,需要一段时间才能响应,我在 VBox 上加载了 ProgressIndicator。 That works ok, but at the moment I get the answer from the API, I try to remove the ProgressIndicator from the VBox, and I get an exception:这工作正常,但目前我从 API 得到答案,我尝试从 VBox 中删除 ProgressIndicator,但出现异常:

Exception in thread "OkHttp Dispatcher" java.lang.IllegalStateException: Not on FX application thread; currentThread = OkHttp http://api.kiwandafood.betas.bolboretapps.com/...
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:444)
    at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
    at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:329)
    at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:221)
    at com.kiwandalabs.forklift.view.MainOverviewController.setLoading(MainOverviewController.java:536)
    at com.kiwandalabs.forklift.model.Cloud$3.onResponse(Cloud.java:223)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

This is the code where I'm getting the exception, at the line where I remove the ProgressIndicator from the VBox:这是我得到异常的代码,在我从 VBox 中删除 ProgressIndicator 的那一行:

public void setLoading(boolean set) {
        if (set) {
            //Activar
            pi = new ProgressIndicator();
            loadingBox.getChildren().add(pi);
            layoutPrincipal.setDisable(true);
            //System.out.println(stackPrincipal.getChildren().indexOf(loadingBox));
        }else {
            //Desactivar
            layoutPrincipal.setDisable(false);
            loadingBox.getChildren().remove(pi);
            pi = null;
        }
} //End setLoading

You need to perform the action(s) on the FX Application Thread eg您需要在 FX 应用程序线程上执行操作,例如

javafx.application.Platform.runLater(new Runnable(){
    //  TODO: update UI components here
});

Edit : Also, my guess would be, when adding the progress indicator, you are in the appropriate thread.编辑:另外,我的猜测是,在添加进度指示器时,您处于适当的线程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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