JavaFX中的进度条

时间:2014-04-03 19:48:34

标签: javafx

我在我的UI中放置了一个进度条和指示器,并尝试在正在执行的一段代码的不同阶段更新进度,但它只是保持空白,然后每当代码块完成时将自身设置为100。 / p>

任何人都知道为什么这不是我的舞台更新阶段?

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);

1 个答案:

答案 0 :(得分:2)

您正在运行FX应用程序线程上的所有内容,因此在整个方法完成之前,呈现线程无法呈现任何内容。 (我猜测NNSE.searchForWords(...)需要时间来执行。)

您需要将代码放入Task,然后调用Task的{​​{1}}方法。然后将updateProgress(...)的{​​{1}}绑定到ProgressBar的{​​{1}}。从单独的线程运行任务。