-1

This code is for progress bar. i'm using this progress bar for a background process but the problem is the progress bar is only visible after the background task completed !!

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

i want to use progress bar for this method!

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();
      }
     }
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Demo
  • 3
  • 7
  • please be more specific with your answer. – Demo Jul 15 '17 at 06:19
  • I ran this code and the progressBar showed when the application loaded. It probably has something to do with the other threads running in your application. – Nash Jul 15 '17 at 07:27
  • please, see the method on which i want this progress bar – Demo Jul 15 '17 at 07:43
  • Works for me too. The problem probably recides in the method calling `ProgBar.porgressBarTest`. Also adding the second code snippet seems completely unnecessary since it has nothing to do with the snippet you're asking about and it doesn't even use `Task`... – fabian Jul 15 '17 at 07:53
  • the second code that i have posted that should be working in background with the code that i have post very first. i need to call the first code for progress bar into second code of exportfile. – Demo Jul 15 '17 at 09:31
  • @fabian when i'm exportingpdf there is a pause how to resolve that problem. like i'm unable to do anything till the time pdf export – Demo Jul 15 '17 at 10:49
  • @fabian can you please have a look to this link, because i'm facing error in the same.Actually i have tried a lot but not getting any result.kindly response over the same https://stackoverflow.com/questions/45140946/javafx-thread-issue/45141072#45141072 – Demo Jul 17 '17 at 10:45

1 Answers1

0

I think you are trying to call exportFile() method after the progressBar loading completes.

Your code looks fine. To call the exportFile() method after progressBar gets loaded you can call setOnSuccededMethod as shown below.

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

This way, the exportFile() function will get called when the progressBar loading finishes. The program might still hang because you are using Desktop class in the exportFile() method. The JAVAFX thread does not work properly when you combine it with Desktop class. JavaFX thread kind of gets blocked. You will need to create a new Thread to open the exported file as shown below.

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

Linked Question: JavaFX thread issue

lambad
  • 1,046
  • 10
  • 21