Java Reference
In-Depth Information
Table 12-5.
JProgressBar Properties (Continued)
Property Name
Data Type
Access
percentComplete
double
Read-only
string
String
Read-write bound
stringPainted
boolean
Read-write bound
UI
ProgressBarUI
Read-write
UIClassID
String
Read-only
value
int
Read-write
Painting JProgressBar Borders
All
JComponent
subclasses feature a
border
property by default, and the
JProgressBar
has a
special
borderPainted
property to easily enable or disable the painting of the border. Calling
the
public void setBorderPainted(boolean newValue)
method with a parameter of
false
turns
off the painting of the progress bar's border. The right-hand progress bar in Figure 12-12
(shown earlier) has its border turned off. The source for its initialization follows:
JProgressBar cJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
cJProgressBar.setBorderPainted(false);
Labeling a JProgressBar
The
JProgressBar
supports the display of text within the center of the component. There are
three forms of this labeling:
•
By default, no label exists.
•
To display the percentage completed [100 × (value-minimum)/(maximum-minimum)],
call the
public void setStringPainted(boolean newValue)
method with a parameter of
true
. This will result in a range from 0% to 100% displayed.
To change the label to a fixed string, call the
public void setString(String newValue)
method and
setStringPainted(true)
. On a vertical progress bar, the string is drawn
rotated, so a longer string will fit better.
•
The left and bottom progress bars in Figure 12-12 demonstrate the fixed label and
percentage label, respectively. The source code to create both progress bars follows:
JProgressBar bJProgressBar = new JProgressBar();
bJProgressBar.setStringPainted(true);
Border border = BorderFactory.createTitledBorder("Reading File");
bJProgressBar.setBorder(border);
JProgressBar dJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
dJProgressBar.setString("Ack");
dJProgressBar.setStringPainted(true);