Java Reference
In-Depth Information
The minimum and maximum values also can be set up by calling the progress bar's
setMinimum( int ) and setMaximum( int ) values with the indicated values.
To update a progress bar, you call its setValue( int ) method with a value indicating how
far along the task is at that moment. This value should be somewhere between the mini-
mum and maximum values established for the bar. The following example tells the
install progress bar in the previous example of a software installation how many files
have been uploaded thus far:
int filesDone = getNumberOfFiles();
install.setValue(filesDone);
In this example, the getNumberOfFiles() method represents some code that would be
used to keep track of how many files have been copied so far during the installation.
When this value is passed to the progress bar by the setValue() method, the bar is
immediately updated to represent the percentage of the task that has been completed.
Progress bars often include a text label in addition to the graphic of an empty box filling
up. This label displays the percentage of the task that has become completed, and you
can set it up for a bar by calling the setStringPainted( boolean ) method with a value
of true . A false argument turns off this label.
Listing 10.4 contains ProgressMonitor , the application shown at the beginning of this
section in Figure 10.13.
LISTING 10.4
The Full Text of ProgressMonitor.java
1: import java.awt.*;
2: import java.awt.event.*;
3: import javax.swing.*;
4:
5: public class ProgressMonitor extends JFrame {
6:
7: JProgressBar current;
8: JTextArea out;
9: JButton find;
10: Thread runner;
11: int num = 0;
12:
13: public ProgressMonitor() {
14: super(“Progress Monitor”);
15:
16: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17: setSize(205, 68);
18: setLayout(new FlowLayout());
19: current = new JProgressBar(0, 2000);
Search WWH ::




Custom Search