JavaFX Threads Update UI and load System in the background

Posted on

Question :

I have a Stage Principal that is my Login:







  • Threaddoesnotstart.

  • Excerptfromlogin:

            //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?


    Answer :

    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();
        return new Scene(vb, 300, 150);        
    public void start(Stage stage){  
        this.stage = stage;
        stage.setScene(createPreloaderScene());       ;
    public void handleStateChangeNotification(StateChangeNotification scn) {
    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) {
          if (pn.getProgress() > 0) {
              noLoadingProgress = false;
    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;
        } else if (pn instanceof StateChangeNotification) {
            //hide after get any state update from application


    I did not see this in your code:


    This line will make the progressbar run.


    Leave a Reply

    Your email address will not be published. Required fields are marked *