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

时间:2017-10-04 11:59:01

标签: javafx-2

我有第一个场景,我点击按钮时有一个注册按钮我尝试在后台线程中建立与我的服务器的连接。现在我只想从我的服务器获得200作为响应代码时才进入下一个场景。 我已经使用Service类作为后台线程。我也创建了一个改变场景的方法,但是我无法理解何时何地调用mehod。

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

1 个答案:

答案 0 :(得分:0)

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