I have been trying to understand how to add a progress bar, I can create one within the GUI I am implementing and get it to appear but even after checking through http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html I am still no clearer on how I can set a method as a task so that I can create a progress bar for running a method. Please can someone try to explain this to me or post an example of a progress bar being used in the GUI with a task being set as a method. Thanks.
-
1One of the answer in this possible duplicate of [Can a progress bar be used in a class outside main?](http://stackoverflow.com/questions/4637215/can-a-progress-bar-be-used-in-a-class-outside-main) may help. – trashgod Jan 18 '12 at 19:44
-
I don't understand. Why you want to **create a progress bar for running a method** (as you said)? The aim of progress bar is to provide to user informations about a task running. So the task told in oracle doc refer to the thread that will manage the progress bar. So create your progress bar, using swing worker(as told in your link), then in your (long?) task update the progress of the bar. The `doInBackground()` method of the SwingWorker is used to manage the progress bar. I advise you to look again at your oracle tuto, explaination are pretty clear and examples reusable. Good luck! – alain.janinm Jan 18 '12 at 19:56
4 Answers
Maybe I can help you with some example code:
public class SwingProgressBarExample extends JPanel {
JProgressBar pbar;
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public SwingProgressBarExample() {
// initialize Progress Bar
pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
add(pbar);
}
public void updateBar(int newValue) {
pbar.setValue(newValue);
}
public static void main(String args[]) {
final SwingProgressBarExample it = new SwingProgressBarExample();
JFrame frame = new JFrame("Progress Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(it);
frame.pack();
frame.setVisible(true);
// run a loop to demonstrate raising
for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
final int percent = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
it.updateBar(percent);
}
});
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
;
}
}
}
}

- 76,500
- 11
- 62
- 80

- 4,761
- 7
- 32
- 54
Your question is a bit vague, but it sounds to me like you want the progress bar to show progress for a specific running method, which I'll call the "work()" method. Unfortunately, there's no way to just pass a reference to your method to a progress bar - your method needs to explicitly tell the progress bar what to display. Here's what I would do:
Make the progress bar's reference available to work() - either pass it in as an argument to work(), or provide an accessor method that your code in work() can call to get a reference to the progress bar.
Inside work(), after you've obtained a reference to the progress bar (which I'll call "pb", call pb.setMinimum(0) and pb.setMaximum(n) where n is the number of steps your method has to get through.
As your method completes each step, call pb.setValue(pb.getValue()+1);
At the end of your method, call pb.setValue(0); to reset the progress bar prior to returning.
Also, if you want your progress bar to display a String message, you first have to call pb.setStringPainted(true), then subsequent calls to pb.setString(string) will show up on the progress bar.

- 4,519
- 1
- 24
- 36
See my answer on another SO question which includes an example of a JProgressBar
which gets updated by using a SwingWorker
. The SwingWorker
is used to execute a long running task in the background (in case of the example it is just a regular Thread.sleep
) and report on progress at certain intervals.
I would also strongly suggest to take a look at the Swing concurrency tutorial for more background info on why you should use a SwingWorker
when performing long-running tasks which interfere with the UI.
A similar example as the one I posted is available in the Swing tutorial about JProgressBar
s, which it also worth looking at
How about this,
JFrame->JButton (BorderLayout.NORTH)
JFrame-> JPanel->JProgressBar (BorderLayout.SOUTH)
You can add button part where ever you like, for example when
Progress progress = ...; state=true; progress.waitFor(); state=false;
private static void daa() { //Frame JFrame frame = new JFrame("Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); frame.setSize(frame.getWidth() + 55, frame.getHeight() + 55); //Button JButton jButton = new JButton("State"); frame.add(jButton, BorderLayout.NORTH); //Progress Bar JProgressBar progressBar = new JProgressBar(); progressBar.setIndeterminate(true); //Text for progress bar JPanel panel = new JPanel(new BorderLayout()); panel.add(progressBar); panel.add(new JLabel("Please wait......."), BorderLayout.PAGE_START); //linking panel.add(progressBar); frame.add(panel, BorderLayout.SOUTH); boolean[] state = {false}; jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { state[0] = !state[0]; state(); } private void state() { if (state[0] == true) { panel.hide(); } else { panel.show(); } } }); }

- 41
- 4