2017-10-04 49 views
0

我有第一个场景,其中有一个注册按钮,单击该按钮我尝试在后台线程中与我的服务器建立连接。现在,只有当我从我的服务器获取200作为响应代码时才想进入下一个场景。 我已经使用Service类作为后台线程。我也创建了一个方法来改变场景,但我无法理解何时何地调用mehod。如何仅在后台线程在javaFx中完成时才转到下一屏幕

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 

回答

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(); 
相关问题