2017-07-15 120 views
-1

此代碼用於進度條。我使用這個進度條進行後臺處理,但問題是進度條僅在後臺任務完成後纔可見!JavaFx進度條

public class ProgBar { 

    public void porgressBarTest(){ 
     ProgressBar progressBar = new ProgressBar(); 
     progressBar.setProgress(0); 
     progressBar.setMinWidth(400); 

     VBox updatePane = new VBox(); 
     updatePane.setPadding(new Insets(30)); 
     updatePane.setSpacing(5.0d); 
     updatePane.getChildren().addAll(progressBar); 

     Stage taskUpdateStage = new Stage(StageStyle.UTILITY); 
     taskUpdateStage.setScene(new Scene(updatePane)); 
     taskUpdateStage.show(); 

     Task<Void> task = new Task<Void>() { 
      public Void call() throws Exception { 
       int max = 200; 
       for (int i = 1; i <= max; i++) { 
        updateProgress(i, max); 
        Thread.sleep(100); 
       } 
       System.out.println("about to close"); 
       return null; 
      } 
     }; 
     progressBar.progressProperty().bind(task.progressProperty()); 
     new Thread(task).start(); 
    } 
} 

我想用這個方法的進度條!

public void exportFile(String fileFormat) throws EngineException { 

    String output = *************; 
    String reportDesignFilePath = ********************; 

    // Save Report In Particular Format 
    try { 
    EngineConfig configure = new EngineConfig(); 
    Platform.startup(configure); 
    IReportEngineFactory reportEngineFactory=(IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); 
    IReportEngine engine = reportEngineFactory.createReportEngine(configure); 
    engine.changeLogLevel(Level.WARNING); 
    IReportRunnable runnable = engine.openReportDesign(reportDesignFilePath); 
    IRunAndRenderTask task = engine.createRunAndRenderTask(runnable); 
    IRenderOption option = new PDFRenderOption(); 
    option.setOutputFormat(fileFormat); 
    option.setOutputFileName(output+fileFormat); 
    task.setRenderOption(option); 
    task.run(); 
    task.close(); 
    } 
    catch(Exception e) { e.printStackTrace(); } 
    // Open Created File 
    File file = new File(output+fileFormat); 
    if (file.exists()) { 
    if (Desktop.isDesktopSupported()) { 
     try { 
      Desktop desktop = Desktop.getDesktop(); 
      desktop.open(file); 
     } 
     catch(IOException e) { 
     e.printStackTrace(); 
     } 
    } 
    } 
} 
+0

請更具體與您的答案。 – Demo

+0

我運行了這段代碼,並在應用程序加載時顯示progressBar。它可能與您的應用程序中運行的其他線程有關。 – Nash

+0

請參閱我希望此進度條的方法 – Demo

回答

0

我想你正在嘗試在progressBar加載完成後調用exportFile()方法。

你的代碼看起來不錯。要在progressBar加載後調用exportFile()方法,您可以調用setOnSuccededMethod,如下所示。

Task<Void> task = new Task<Void>() { 
      public Void call() throws Exception { 
       int max = 200; 
       for (int i = 1; i <= max; i++) { 
        updateProgress(i, max); 
        Thread.sleep(100); 
       } 
       System.out.println("about to close"); 
       return null; 
      } 
     }; 
     progressBar.progressProperty().bind(task.progressProperty()); 

     task.setOnSucceeded(e -> { 

      exportFile(); 

     }); 

     task.setOnFailed(e -> { 
      //more things 
     }); 
     new Thread(task).start(); 

這樣,當progressBar加載完成時,將調用exportFile()函數。 由於您在exportFile()方法中使用Desktop類,程序可能仍會掛起。當您將其與Desktop類結合使用時,JAVAFX線程無法正常工作。 JavaFX線程被阻塞。您將需要創建一個新的線程來打開導出的文件,如下所示。

public void exportFile(String fileFormat) throws Exception{ 

//upper portion of Code 

     File fileToOpen = new File("LocationOfFile"); 
     if(Desktop.isDesktopSupported()) { 
      new Thread(() -> { 
       try { 
        Desktop.getDesktop().open(fileToOpen); 
        System.out.println("FileOpened Successfully"); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      }).start(); 
     } 


     System.out.println("exportFile finishing"); 
    } 

鏈接的問題:JavaFX thread issue