Java Reference
In-Depth Information
Using an Indeterminate JProgressBar
Some tasks don't have a fixed number of steps, or they do have a fixed number of steps, but you
don't know what that number is until after all the steps are done. For this type of operation, the
JProgressBar offers an indeterminate mode where the bar within the JProgressBar bounces
back and forth from side to side, or top to bottom, depending on the direction of the progress
bar. To enable this mode, just call the public void setIndeterminate(boolean newValue)
method with a value of true . Figure 12-13 shows what an indeterminate progress bar looks like
at different times. The length of the sliding box is one-sixth the available space and seems to
not be settable.
Figure 12-13. Sample indeterminate JProgressBar
Stepping Along a JProgressBar
The main usage of the JProgressBar is to show progress as you step through a series of operations.
Normally, you set the minimum value of the progress bar to zero and the maximum value to
the number of steps to perform. Starting with a value property of zero, you increase the value
to the maximum as you perform each step. All these operations imply multithreading, which is,
in fact, absolutely necessary. In addition, when updating the progress bar's value, you need
to remember to update it only from within the event dispatching thread (with the help of
EventQueue.invokeAndWait() , if appropriate, as described in Chapter 2).
The process of having a progress bar step through its range is as follows:
Initialize it. This is the basic process of creating a JProgressBar with the desired orien-
tation and range. In addition, perform any bordering and labeling here.
1.
JProgressBar aJProgressBar = new JProgressBar(0, 50);
aJProgressBar.setStringPainted(true);
2.
Start up the thread to perform the desired steps. Probably as the result of performing
some action on the screen, you'll need to start the thread to do the work the progress
bar is reporting. You need to start a new thread so that the user interface remains
responsive.
Thread stepper = new BarThread (aJProgressBar);
stepper.start();
3.
Perform the steps. Ignore updating the progress bar, and instead write the appropriate
code to perform each step.
static class BarThread extends Thread {
private static int DELAY = 500;
JProgressBar progressBar;
Search WWH ::




Custom Search