Java Reference
In-Depth Information
ProgressMonitor monitor = new ProgressMonitor(
parent, "Loading Progress", "Getting Started...", 0, 200);
Using a ProgressMonitor
Once you've created the ProgressMonitor , you need to begin the task whose progress is being
monitored. As the task completes one or many steps, the ProgressMonitor needs to be notified of
the task's progress. Notification is done with a call to the public void setProgress(int newValue)
method, where the argument represents the progress completed thus far and the newValue
needs to be in the minimum...maximum range initially specified. This progress value needs to be
maintained outside the ProgressMonitor , because you can't ask the monitor how much progress
has been made (no public int getProgress() method of ProgressMonitor exists). If the progress
value were maintained in a variable named progress , the following two lines would update the
progress value and notify the ProgressMonitor .
progress += 5;
monitor.setProgress(progress);
Note It's possible that multiple calls to setProgress() may not advance the progress bar in the option
pane. The changes to the progress setting must be enough to make the progress bar advance at least one
pixel in length. For instance, if the minimum and maximum settings were zero and 2 billion, increasing the
progress setting 1,000 times by 5 would have no visible effect on the progress bar, because the fractional
amount would be negligible.
The progress setting could represent the number of files loaded thus far, or the number of
bytes read in from a file. In addition to updating the count, you should update the note to
reflect the progress. If the difference between the minimum and maximum arguments used in the
ProgressMonitor constructor were 100, then the current progress could be viewed as a percentage
of the task. Otherwise, the progress property merely represents the progress completed so far.
monitor.setNote("Loaded " + progress + " files");
It's the responsibility of the executing task to check whether the user pressed the Cancel
button in the ProgressMonitor dialog box. If the task is canceled, the ProgressMonitor automat-
ically closes the dialog box, but the task must actively check for the change by adding a simple
check at the appropriate place or places in the source:
if (monitor.isCanceled()) {
// Task canceled - cleanup
...
} else {
// Continue doing task
...
}
 
Search WWH ::




Custom Search