3

I need a very simple (skeletal) tutorial on progress bars for developing a GUI in Java. The progress bar can be "indeterminate" and ideally have an updateable text label.

Here is some skeletal code that I hope someone can expand upon just enough to get something working.

class MyFunView extends FrameView {

  // class vars and other stuff...

  @Action
  public void excitingButtonPressed() {

     /* I have n files to download */
     // Download file 1, set progress bar status to "downloading file 1"
     // ...
     // Download file n, set progress bar status to "downloading file 2"

  }

} 

Basically, how and what do I wrap my download code in? (I understand there is some kind of subclass of Task required?)

Thanks much in advance.

EDIT:

We all only have so much precious time to be alive, and no approach can be done in as little code or time as I would have hoped, so I will not be using a progress bar.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bill
  • 2,319
  • 9
  • 29
  • 36
  • 1
    *"I need a very simple (skeletal) tutorial on progress bars"* Here you go: [How to Use Progress Bars](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html). *"Here is some skeletal code that I hope someone can expand upon"* What's your budget? – Andrew Thompson May 02 '12 at 22:19
  • 1
    BTW - `downloading file..` look to [`ProgressMonitorInputStream`](http://docs.oracle.com/javase/7/docs/api/javax/swing/ProgressMonitorInputStream.html) for this. It is made for it, and should give an indication of the real progress. – Andrew Thompson May 02 '12 at 22:27
  • @B. VB. wrote in EDIT [We all only have so much precious time to be alive, and no approach can be done in as little code or time as I would have hoped, so I will not be using a progress bar.](http://www.java2s.com/Code/Java/Swing-JFC/CatalogSwing-JFC.htm), because I think that you forgot to click .... – mKorbel May 03 '12 at 07:14

2 Answers2

6

I need a very simple (skeletal) tutorial on progress bars for developing a GUI in Java.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

You can use SwingWorker in conjunction with a JProgressBar.

The other alternative is to execute your task (e.g. Runnable) and periodically update the JProgressBar. Beware though, that in Swing any UI manipulations have to be done on the EDT. SwingWorker has a nice property of taking care of this detail. Your own solution will have to use SwingUtilities.invokeLater,. for example:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        progressBar.setValue(progressCounter++);
    }
});

Lot of code for a stupid thing, isn't it? Maybe the SwingWorker with a PropertyChangeListener is a better solution in the end...

Anonymous
  • 18,162
  • 2
  • 41
  • 64
  • "_Lot of code for a stupid thing, isn't it?_" - Yup, so screw the progress bar.. not worth it, sorry users. – Bill May 02 '12 at 22:36
  • Well, it can still be in the `PrepertyChangeListener` - but with a different kind of boilerplate (the listener itself, no `invokeLater`). – Anonymous May 02 '12 at 22:38
  • @Anonymous I think that OP's needed to explaining [this one](http://stackoverflow.com/a/10392153/714968) or [this one](http://stackoverflow.com/a/10395968/714968), hell why there are only my up_votes – mKorbel May 02 '12 at 22:38