仅当后台线程在 javaFx 中完成时如何转到下一屏幕

How to go to Next Screen only when the Background thread is completed in javaFx

我有第一个场景,在这个场景中我有一个注册按钮,点击按钮我尝试在后台线程中建立到我的服务器的连接。现在,只有当我从服务器收到 200 作为响应代码时,我才想去下一个场景。 我已经使用 Service class 作为背景 thread.I 也创建了一个方法来改变场景,但我无法理解何时何地调用方法。

public class MainController implements Initializable {

    int responseCodeFromServer;;
    // creating background thread
    private Service<Void>backgroundThread;

        backgroundThread = new Service<Void>() 
        {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() throws Exception 
                    {

                        // Now here we will try to establish the connection with the Server
                        EstablishServerConnection obj = new EstablishServerConnection();
                        responseCodeFromServer = obj.establishConnectionToServer(registrationBeanObj);
                        System.out.println("Response Code received in UI thread "+ responseCodeFromServer);
                        if(responseCodeFromServer == 200)
                        {
                            updateMessage("All Ok");    
                            // now when we get response code as 200 then we need to take the user to the next window

                        }
                        else
                        {
                            updateMessage("Server Issue");
                        }
                        // TODO Auto-generated method stub
                        return null;
                    }
                };
            }
        };

    // we will define here what will happen when this background thread completes its job successfully (we can also try for failed or cancelled events) 
        backgroundThread.setOnSucceeded(new EventHandler<WorkerStateEvent>() 
        {

            @Override
            public void handle(WorkerStateEvent event) {
                // TODO Auto-generated method stub
                if(responseCodeFromServer ==  200)
                {
                    System.out.println("Done"); 
                }

                // It is a good idea to unbind the label when our background task is finished
                status.textProperty().unbind();
            }
        });
    // we need to bind status label text property to the message property in our background thread
        status.textProperty().bind(backgroundThread.messageProperty());
    // we need to start our background thread 
        backgroundThread.restart();     

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        System.out.println("Hello World");
    }
    public void goToProductKey(ActionEvent event) throws IOException
    {
        Parent goToProductKeyParent = FXMLLoader.load(getClass().getResource("ProductKeyFXML.fxml"));
        Scene  goToProductKeyScene = new Scene(goToProductKeyParent);

        // This line gets the stage Information 
        Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
        window.setScene(goToProductKeyScene);
        window.show();

    }

My Question is I want to go to next scene only when i get 200 as response code from my server.I am new to JavaFX
        backgroundThread.setOnSucceeded(new EventHandler<WorkerStateEvent>() 
        {

            @Override
            public void handle(WorkerStateEvent event) 
            {
                // TODO Auto-generated method stub
                if(responseCodeFromServer ==  1)
                {
                    Parent goToProductKeyParent = null;
                    try {
                        goToProductKeyParent = FXMLLoader.load(getClass().getResource("ProductKeyFXML.fxml"));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Scene  goToProductKeyScene = new Scene(goToProductKeyParent);
                    // This line gets the stage Information 
                    Stage window = (Stage) rootPane.getScene().getWindow();
                    //Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
                    window.setScene(goToProductKeyScene);
                    window.show();                  
                }
                // It is a good idea to unbind the label when our background task is finished
                status.textProperty().unbind();
            }
        });
    // we need to bind status label text property to the message property in our background thread
        status.textProperty().bind(backgroundThread.messageProperty());
    // we need to start our background thread 
        backgroundThread.start();