2017-10-04 4 views
0

J'ai la première scène dans laquelle j'ai un bouton d'enregistrement en cliquant sur le bouton J'essaie d'établir une connexion à mon serveur dans un fil de fond. Maintenant, je veux aller à la scène suivante seulement quand je reçois 200 comme code de réponse de mon serveur. J'ai utilisé la classe Service pour thread d'arrière-plan. J'ai également créé une méthode pour changer la scène mais je ne suis pas capable de comprendre où et quand appeler mehod.Comment aller à l'écran suivant uniquement lorsque le fil d'arrière-plan est terminé en 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 

Répondre

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