2

I am new to Java FX and decided to use Scene Builder and Intellij, which creates a controller class. I am having an issue with a method (start() )in the controller that updates ProgressBars and Labels frequently.

The start() method executes a method (anal1.intializeFileList();) that does calculations and does not involve JavaFX at all. anal1.intializeFileList() updates its progress to static members of a class Status. The start() method reads the values of the members to determine the status. I am trying to report the status to ProgressBars and Labels. The Labels and the ProgressBars are not updating except at the very end of the method when I set the ProgressBars to 100% and the Label indicating that the work is done. I tried to also run the only code using JavaFX in a Runnable and passed it to Platform.runlater, and that did not work. I also tried to run the code as a Task by passing it to execuctorService.execute and that also did not work. I also tried also to reduce the sleep time in the while loop and that did not work.

Here are the status bars and labels I am trying to update which are declared in the Controller

@FXML
private ProgressBar populateProgress;
@FXML
private ProgressBar sortProgress;
@FXML
private ProgressBar dispoProgress;
@FXML
private Label initLabel;

Here is the code. The logic and code is working and I confirmed it by the various println statements, expect the labels and the progress bars are not updating. Any ideas?

Thank You, David

 @FXML
    private void start() {
    
        // adding ActionEvent as a argument to this method did not work

        initLabel.setText("Please wait");

        class runDB implements Runnable {

            @Override
            public void run() {
                anal1.intializeFileList();

            }
        }
        // runAnalysisStatus implements Runnable with Platform.runlater did not update GUI


        /**
         * updates the statuses in the report options tab every 100ms
         */

        Stats.notDone = true;
        Stats.init = false;
        Stats.populate = false;
        Stats.sort = false;
        Stats.dispo = false;
        double progress = 0.0;
        populateProgress.setProgress(0);
        sortProgress.setProgress(0);
        dispoProgress.setProgress(0);
        // start analysis
        Thread anal = new Thread(new runDB());
        anal.start();


        initLabel.setText("Intitializing report");
        // debug only

        System.out.println("*******Initializing Status B4 While **");

        while (Stats.notDone) {
            //debug only
           System.out.println("*******Status Pre sleep ******");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
           
            
            if ((!Stats.init) && (Stats.notDone)) {
                // debug only
                System.out.println("*****Status intializing *** In status");
            } else {

                initLabel.setText("Refer to status below");
                // debug only
                System.out.println("***** Status init done Text should say Refer Status...");

                //populateFileList section
                if ((!Stats.populate) && (Stats.notDone)) {
                    // debug only
                    System.out.println("****** Status in populate ******");
                    // since n2 can not be non-zero first than zero later, this should work
                    // debug only setting to 55 instead of zero for n2=0
                    progress = Stats.n1 == 0 ? 0.55F : (double) Stats.nc / Stats.n1;
                    //debg only
                    System.out.println("populate progress is: "+progress);
                    populateProgress.setProgress(progress);
                    System.out.println("**** Progress Bar should be moving ******");
                } else {

                    populateProgress.setProgress(1.00);
                    // debug only
                    System.out.println("******* Populate should be 100");

                    // sort progress section;
                    progress = 0.0;
                    if ((!Stats.sort) && (Stats.notDone)) {
                        // debug only
                        System.out.println("********* Status in sort ******* in Status thread");
                        progress = Stats.nc / Stats.n2;
                        sortProgress.setProgress(progress);
                        //debug only
                        System.out.println("********** Progress bar should be moving **** In status tread");
                    } else {
                        sortProgress.setProgress(1.00);
                        // dispo progress section
                        if ((!Stats.dispo) && (Stats.notDone)) {
                            // debug Only
                            System.out.println("***** Status in Dispo ******** In Status Tread");
                            progress = Stats.nc / Stats.n2;
                            dispoProgress.setProgress(progress);
                        } else {
                            dispoProgress.setProgress(1.00);
                        }
                    }
                }
            }

            

        }
        
        // debug: all these lines work:
        System.out.println(">>>Done Status ***** Entered status thread ");
        // end debug
        populateProgress.setProgress(1.00);
        sortProgress.setProgress(1.00);
        dispoProgress.setProgress(1.00);
        initLabel.setText("Analysis Done");
David R.
  • 23
  • 5
  • 6
    You're both looping on and sleeping the FX thread. Either one of those things would cause your UI to become unresponsive. Check out [Concurrency in JavaFX](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm) and the [javafx.concurrent](https://openjfx.io/javadoc/14/javafx.graphics/javafx/concurrent/package-summary.html) package. – Slaw Jul 19 '20 at 02:51
  • 1
    Thank you. That is a useful resource to me. I shall change my code accordingly. – David R. Jul 19 '20 at 04:01

0 Answers0