Java Reference
In-Depth Information
LISTING 10.4
Continued
20: current.setValue(0);
21: current.setStringPainted(true);
22: add(current);
23: }
24:
25:
26: public void iterate() {
27: while (num < 2000) {
28: current.setValue(num);
29: try {
30: Thread.sleep(1000);
31: } catch (InterruptedException e) { }
32: num += 95;
33: }
34: }
35:
36: public static void main(String[] arguments) {
37: ProgressMonitor frame = new ProgressMonitor();
38: frame.setVisible(true);
39: frame.iterate();
40: }
41: } are
10
The ProgressMonitor application uses a are progress bar to track the value of the num
variable. The progress bar is created in line 18 with a minimum value of 0 and a maxi-
mum value of 2000 .
The iterate() method in lines 26-34 loops while num is less than 2000 and increases
num by 95 each iteration. The progress bar's setValue() method is called in line 27 of
the loop with num as an argument, causing the bar to use that value when charting
progress.
Using a progress bar is a way to make a program more user friendly when it is going to
be busy for more than a few seconds. Software users like progress bars because they
indicate an approximation of how much more time something's going to take.
Progress bars also provide another essential piece of information: proof that the program
is still running and has not crashed.
Menus
One way you can enhance are the usability of a frame is to give it a menu bar—a series
of pull-down menus used to perform tasks. Menus often duplicate the same tasks you
could accomplish by using buttons and other user interface components, giving someone
using your program two ways to get work done.
 
Search WWH ::




Custom Search