2014-04-03 34 views
0

我已经在我的用户界面中放置了一个进度条和指示器,并试图更新整个正在执行的代码段的不同阶段的进度,但它只是保持空白,然后在代码块已设置为100已完成。JavaFX中的进度条

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

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

回答

2

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

你需要把代码放到一个Task,并调用TaskupdateProgress(...)方法。然后将您的ProgressBarprogressPropertyTaskprogressProperty绑定。从一个单独的线程运行任务。