JavaFX Threads Update UI and load System in the background

4

I have a Stage Principal that is my Login:

In%ofthisstageIhaveaFXMLandaRegionwiththeProgressIndicatorproperty

AftersuccessfullyloggingintotheapplicationtheintentionwouldbetodisplaythissetVisible(false)andRegionwhileathreadstartedtheapplication,butwhathappensisthatafterloggingin/em>startsrunningbyloadingtheapplication,butProgressIndicatorandRegionarenotvisible.

Mythread

Taskt=newTask(){@OverrideprotectedObjectcall()throwsException{Platform.runLater(()->{try{newSisgeFX().start();}catch(IOExceptionex){Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE,null,ex);}});returnnull;}};region.visibleProperty().bind(t.runningProperty());pi.visibleProperty().bind(t.runningProperty());Threadth=newThread(t);th.start();

Itrieddoingtwothreads:onetoloadthesystemandanothertoupdateProgressIndicatorwhilethesystemisnotloaded,buttonoavail,doesnotgenerateProgressIndicator,I'vetrieditinseveralways.

WhatInoticedinmymanyattempts:

  • Threaddoesnotstart.

  • Excerptfromlogin:

    @FXMLprivatevoidsysLogin(){Stringuser=ctfUserLogin.getText();Stringpass=ctfPassLogin.getText();LoginDAOloginDAO=DAOFactory.make(LoginDAO.class);Loginlogin=loginDAO.getLogin(user,pass);if(login!=null){runThread();//aquichamoaThreadpostadoacima.ctfPassLogin.setStyle(null);ctfUserLogin.setStyle(null);}else{ctfPassLogin.clear();ctfUserLogin.clear();ctfPassLogin.setStyle("-fx-border-color:red;");
            ctfUserLogin.setStyle("-fx-border-color:red;");
            //new ShakeTransition(vBox).play();
            new WobbleTransition(vBox).play();
            //new TadaTransition(vBox).play();
        }
    }
    

    After successfully logging in to Exception while the system loads in the background?

        
    asked by anonymous 22.11.2014 / 02:41

    2 answers

    1

    An elegant way to solve your problem is to create a Preloader, considered a good practice programming in JavaFX. Preloader is especially useful for improving the user experience when it needs to wait for heavy operations to complete before the application starts. There is even a way to make a Preloader Login, explained in detail in this Oracle tutorial - Section 9.3.5 .

    However, in the examples above, loading is done before login and not later. (Maybe there is a workaround to achieve this effect)

    (NetBeans) Creating a Prealoder:

  • Create a Preloader in New Project > JavaFX > JavaFX Preloader;
  • Right click on the project go to Properties > Run > Check "Use Preloader" > Click "Browse" > "Choose Preloader from Project" > Select your Preloader folder;
  • Create the public void init () method in your main application.
  • Within the init () method, you can load all the load required by your application, punctuating the progress using the notifyPreloader(new ProgressNotification(0.10)) // Para 10%;

    Here is an example of a preloader with a simple progress bar:

    public class AppPreloader extends Preloader{
    
    private Stage stage;
    private ProgressBar bar;
    private boolean noLoadingProgress = true;
    
    private Scene createPreloaderScene() {
        bar = new ProgressBar();
    
        VBox vb = new VBox();
        vb.setAlignment(Pos.CENTER);
        bar.setPrefWidth(150);
        vb.getChildren().addAll(bar);
    
        return new Scene(vb, 300, 150);        
    }
    
    @Override
    public void start(Stage stage){  
        this.stage = stage;
        stage.setScene(createPreloaderScene());        
        stage.show();
    }
    
    @Override
    public void handleStateChangeNotification(StateChangeNotification scn) {
    
    }
    
    @Override
    public void handleProgressNotification(ProgressNotification pn) {
        //application loading progress is rescaled to be first 50%
        //Even if there is nothing to load 0% and 100% events can be
        // delivered
        if (pn.getProgress() != 1.0 || !noLoadingProgress) {
          bar.setProgress(pn.getProgress()/2);
          if (pn.getProgress() > 0) {
              noLoadingProgress = false;
          }
        }
    }
    
    @Override
    public void handleApplicationNotification(PreloaderNotification pn) {
        if (pn instanceof ProgressNotification) {
           //expect application to send us progress notifications 
           //with progress ranging from 0 to 1.0
           double v = ((ProgressNotification) pn).getProgress();
           if (!noLoadingProgress) {
               //if we were receiving loading progress notifications 
               //then progress is already at 50%. 
               //Rescale application progress to start from 50%               
               v = 0.5 + v/2;
           }
           bar.setProgress(v);            
        } else if (pn instanceof StateChangeNotification) {
            //hide after get any state update from application
            stage.hide();
        }
    }
    }
    
        
    25.06.2017 / 17:19
    0

    I did not see this in your code:

    progressBar.progressProperty().bind(task.progressProperty());
    

    This line will make the progressbar run.

        
    23.08.2015 / 14:13