JavaFX进度条用于复杂的后台任务

时间:2014-10-31 03:36:20

标签: javafx progress-bar task

如何实现与ProgressBar相关的'updateProgress'相关逻辑,以指示复杂背景任务的滚动性能,如下所示。 这意味着任务相关的服务方法是如此复杂,并且很难用该方法逐步细节并实现'updateProgress'相关逻辑。 如果有人有任何想法,请告诉我。感谢。

@FXML 
private ProgressBar progressBar;

@Autowired
private DataExportService dataExportService;

private List<Device> deviceList; // User selected device list for export 

/*
 * Export user selected data
 */ 
@FXML 
public void doExport() {

 // Defile the data export concurrent Task
 final Task taskExport = new Task<Void>() {

      @Override protected Void call() throws Exception {

          if(deviceList.size() > 0) {

             try {
                    // How can I implement ProgressBar related 'updateProgress' logic in here ?
                    // Note: It is so hard to implement ProgressBar related indicator on in side the 
                    //       'exportData()' methods because it is too complicated and it had several 
                    //        steps ex: Filter Device related all data, Build a SQL script,
                    //                  Write SQL Script to file etc.  

                    dataExportService.exportData(deviceList);

             } catch (DataExportException e) {
                    logger.error(e.getMessage());
             }

           }

          return null;
      }
 };

 // Set task property to ProgressBar
 progressBar.progressProperty().bind(taskExport.progressProperty());

 // Start data export task
 new Thread(taskExport).start();
} 

1 个答案:

答案 0 :(得分:0)

你正在使一切顺利,但你错过了updateProgress()的召唤 以下是代码示例:

Task t = new Task() {
    @Override
    protected Void call() throws Exception {
        int maximum = 20;
        for (int i = 0; i < maximum; i++) {
            // Here you update the progress each time
            updateProgress(i, maximum);
        }
        return null;
    }
};

快乐编码,
Kalasch