简体   繁体   中英

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

I'm trying to add a project with java fx. On this project I have a stack pane containing an AnchorPane with all the controls, and an empty VBox on top.

When I perform call to a remote API, which will take a while to respond, I load a ProgressIndicator on the VBox. 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:

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:

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

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.

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