-1
private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
     if ("PDF(.pdf)".equals(jComboBox1.getSelectedItem())) {
                query = queryPane.getText();
                filePath = selectedFolder.getText();
                new DataBaseToPDF(filePath, query);
             }
       }

This is a code fragment to export data to a pdf file(DataBaseToPDF(filePath,query)) on a button click. While the process runs in background, I would like to display a progress bar till the export completes. I have come across many examples which tells how to create a JProgressBar but none of them tell how to link it to the running java application.

Tell me how to do it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ritesh
  • 314
  • 7
  • 19
  • 1
    Does the tool that is being sun **give** any indication of progress before it is finished? Many don't, in which case, it is not possible. Just show the user an indeterminate progress bar and be done with it. *"Tell me how to do it."* If you are ordering me to do things, I require $. How about you rephrase that as a question like "How to do this?". – Andrew Thompson May 03 '13 at 13:31
  • 2
    Your task (exporting data to PDF) is time consuming. Since swing runs on single (EDT) thread, your GUI will be "frozen" during that process (JProgressBar won't show anything until process ends). To shorten: Have a look at this question: http://stackoverflow.com/questions/4637215/can-a-progress-bar-be-used-in-a-class-outside-main/4637725#4637725 And at SwingWorker: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html – Branislav Lazic May 03 '13 at 13:33
  • @AndrewThompson- Im sorry for being rude. I didn't mean that. And yes , as an indication I have added a JOptionPane which gives a message when the export is complete. – Ritesh May 03 '13 at 13:43
  • It would be much better if you rephrase your question... – Vishal K May 05 '13 at 17:58

1 Answers1

4
  • use ProgressMonitorInputStream, more in Oracle tutorial How to Use Progress Bars

  • or to create own progress monitor,

    1. File has size in bytes

    2. divide this size to 100, move with progress aftre buffer write bytes to File

    3. notice all updates to Swing GUI must be done on EDT, wrapped in invokeLater

    4. by override publish() / progress() by using SwingWorker

mKorbel
  • 109,525
  • 20
  • 134
  • 319