Updating your UI and forcibly waiting before continuing JavaFX

后端 未结 2 819
攒了一身酷
攒了一身酷 2020-11-29 13:20

I show here an image of my welcome scene.

The current behavior of the Create New Project button is shown here:

Stage stage = (Stage)(         


        
相关标签:
2条回答
  • 2020-11-29 13:43

    You should try adding in ControlsFX this will help.

    You can do the following:

       Service<T> service = new Service<T>(){
           @Override
           protected Task<T> createTask() {
           return new Task<T>() {
              @Override
              protected T call() throws Exception {
            //Do your processing here.                      }
              };
          }
        };
        service.setOnSucceeded(event -> {//do your processing});
        service.setOnFailed(event -> {//do your processing});              
        ProgressDialog pd = new ProgressDialog(service);
        pd.setContentText("Please wait while the window loads...");
        pd.initModality(Modality.WINDOW_MODAL);
        pd.initOwner(stage);
        service.start();
    

    This will put your code on the background thread. ControlsFX dialogs start and stop with the service.

    0 讨论(0)
  • 2020-11-29 14:04

    Instead of sleeping the thread, use a PauseTransition and load your new scene after the pause has finished.

    createNewProject.setOnAction(event -> {
        statusText.setText("Please wait...");
        PauseTransition pause = new PauseTransition(
            Duration.seconds(1),
        );
        pause.setOnFinished(event -> {
            Parent root = FXMLLoader.load(
                getClass().getResource(
                    "/view/scene/configure/NewProjectConfigureScene.fxml"
                )
            );
            Scene scene = new Scene(root);
            stage.setTitle("Configure New Project Settings");
            stage.setScene(scene);
            stage.sizeToScene();    
        });
        pause.play();
    });
    

    The above code assumes you have just a single stage, so you resize your "Welcome" stage to become the "Configure New Project Settings" stage.

    0 讨论(0)
提交回复
热议问题