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