user3469624
user3469624

Reputation: 25

Progress bar in JavaFX

I have placed a progress bar and indicator in my UI and attempted to update the progress at different stages throughout a section of code being executed however it just stays blank and then sets itself to 100 whenever the code block has been completed.

Anyone know why this is not updating stage my stage?

private void onSearchButtonClicked(ActionEvent event) throws InstantiationException, IllegalAccessException {
    //progressBar.setVisible(true);
    //progressIndicator.setVisible(true);
    progressBar.setProgress(1);
    progressIndicator.setProgress(1);
    //mainWindow.getChildren().addAll(pb, pi);
    try {
            wordOne = NNSE.searchForWords(wordOneText.getText());

                progressBar.setProgress(10);
                progressIndicator.setProgress(10);

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    try {
    wordTwo = NNSE.searchForWords(wordTwoText.getText());

                progressBar.setProgress(30);
                progressIndicator.setProgress(30);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        distance = wordNetMeasures.searchForWord(wordOneText.getText(), wordTwoText.getText());
        linDisance = wordNetMeasures.linMethod(wordOneText.getText(), wordTwoText.getText());
        leskDistance = wordNetMeasures.leskMethod(wordOneText.getText(), wordTwoText.getText());
        euclideanDistance = NNSE.calculateDistance(wordOne, wordTwo);

                        progressBar.setProgress(50);
                        progressIndicator.setProgress(50);

        //System.out.println("distance = " + euclideanDistance);
        wordNetPercentage = wordNetMeasures.calculatePercentageForWordNetPair(distance);
        wordNetPercentageLin = wordNetMeasures.calculatePercentageForWordNetPair(linDisance);
        wordNetPercentageLesk = wordNetMeasures.calculatePercentageForWordNetPair(leskDistance);

                    ProjectProperties.getInstance().setWordOneText(wordOneText.getText());
                    ProjectProperties.getInstance().setWordTwoText(wordTwoText.getText());

        //System.out.println("word net percentage" + wordNetPercentage);
        nnsePercentage = NNSE.calculateSimilarityPercentageForNNSEPair(euclideanDistance);

                    //setting properties for these results
                    //ProjectProperties properties = new ProjectProperties();
                    String wordNetDistance = String.valueOf(df.format(distance));
                    ProjectProperties.getInstance().setPathWordNetText(wordNetDistance);
                    ProjectProperties.getInstance().setLinWordNetText((String.valueOf(df.format(linDisance))));
                    ProjectProperties.getInstance().setLeskWordNetText((String.valueOf(df.format(leskDistance))));

                        progressBar.setProgress(70);
                        progressIndicator.setProgress(70);

                    NNSEAccuraryLin = NNSE.calculateNNSEAccuracy(linDisance, euclideanDistance);
                    NNSEAccuracyLesk = NNSE.calculateNNSEAccuracy(leskDistance, euclideanDistance);
        NNSEAccuracy = NNSE.calculateNNSEAccuracy(distance, euclideanDistance);

                    ProjectProperties.getInstance().setPathNNSEText((String.valueOf(df.format(euclideanDistance))));
                    ProjectProperties.getInstance().setLinNNSEText((String.valueOf(df.format(euclideanDistance))));
                    ProjectProperties.getInstance().setLeskNNSEText((String.valueOf(df.format(euclideanDistance))));

                    ProjectProperties.getInstance().setPathNNSEAccuracyText((String.valueOf(pf.format(NNSEAccuracy))));
                    ProjectProperties.getInstance().setLinNNSEAccuracyText((String.valueOf(pf.format(NNSEAccuraryLin))));
                    ProjectProperties.getInstance().setLeskNNSEAccuracyText((String.valueOf(pf.format(NNSEAccuracyLesk))));

                        progressBar.setProgress(80);
                        progressIndicator.setProgress(80);

                    Database databaseConnection = new Database();
    try {
        databaseConnection.getConnection();
        databaseConnection.addWordNetToDatabase(NNSEAccuracy, ProjectProperties.getInstance().getWordOneText() + " ," + ProjectProperties.getInstance().getWordTwoText(), distance);
        databaseConnection.addNNSEToDatabase(NNSEAccuracy, ProjectProperties.getInstance().getWordOneText() + " ," + ProjectProperties.getInstance().getWordTwoText(), euclideanDistance);
    } catch (SQLException ex) {
        Logger.getLogger(PairSearchPageController.class.getName()).log(Level.SEVERE, null, ex);
    }
             progressBar.setProgress(100);
             progressIndicator.setProgress(100);

Upvotes: 0

Views: 1216

Answers (1)

James_D
James_D

Reputation: 209694

You're running everything on the FX Application Thread, so the rendering thread can't render anything until the entire method is complete. (I'm guessing that NNSE.searchForWords(...) takes time to execute.)

You need to put the code into a Task, and call the Task's updateProgress(...) method. Then bind your ProgressBar's progressProperty to the Task's progressProperty. Run the Task from a separate thread.

Upvotes: 3

Related Questions