views:

87

answers:

2

Hi, how do I display the progress in a jProgressBar when I write something in the database?

Thanks

A: 

On your server, you must collect the progress data and supply it in a way that jProgressBar can query it.

Aaron Digulla
+1  A: 

You would typically use a SwingWorker to update the JProgressBar from the Event Dispatch thread whilst performing the database write operations on the background thread. For example:

// Create progress bar to represent 10 items we wish to write to
// the database (hence min := 0, max := 10);
JProgressBar pBar = new JProgressBar(0, 10);

// TODO: Add progress bar to panel and display.

// Invoke SwingWorker to perform database write operations on
// background thread.
new SwingWorker<Void, Integer>() {
  /**
   * Called on background worker thread.  Performs DB write operations.
   */
  public Void doInBackground() {
    for (int i=0; i<10; ++i) {
      // TODO: Perform DB write operation here.

      // Publish progress so far back to Event Dispatch Thread.
      publish(i);
    }
  }

 /**
  * Called on EDT with intermediate progress result(s).
  */
 protected void process(List<Integer> chunks) {
   if (!chunks.isEmpty()) {
     int progress = chunks.get(chunks.size() - 1);
     pBar.setValue(progress);
   }
 }      

 /**
  * Called on EDT when DB write task has completely finished.
  */
 protected void done() {
   pBar.setValue(pBar.getMaximum());
   // TODO: Hide progress bar, etc.
 }
}.execute()
Adamski