JavaFx进度条

时间:2017-07-15 05:23:27

标签: java 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();
      }
     }
    }
}

1 个答案:

答案 0 :(得分: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