Java Reference
In-Depth Information
After the ProgressMonitor displays the pop-up window, the user can do one of two things.
The user can watch the ProgressMonitor display to see how much of the task has been completed;
when the task is done, the ProgressMonitor 's display automatically disappears. Or, if the user
selects the Cancel button, this tells the ProgressMonitor that the task needs to be canceled. To
detect the cancellation, the task needs to check the ProgressMonitor periodically to see if the
user canceled the task's operation. Otherwise, the task will continue.
The pop-up window that the ProgressMonitor class displays is a JOptionPane with a
maxCharactersPerLineCount property setting of 60, allowing the option pane to automatically
word wrap any displayed messages. The option pane is embedded within a nonmodal JDialog
whose title is “Progress . . .”. Because the JDialog isn't modal, a user can still interact with the
main program. The JOptionPane for a ProgressMonitor will always get an informational icon
within its icon area.
In addition, the message area of the option pane consists of three objects:
At the top of the message area is a fixed message that stays the same throughout the life
of the JOptionPane . The message can be a text string or an array of objects just like the
message property of JOptionPane .
In the middle of the message area is a note or variable message that can change as the
task progresses.
At the bottom of the message area is a progress bar ( JProgressBar component) that fills
as an increasing percentage of the task is completed.
The button area of the option pane shows a Cancel button.
Creating a ProgressMonitor
When you create a ProgressMonitor , there are five arguments to the single constructor:
public ProgressMonitor(Component parentComponent, Object message, String note,
int minimum, int maximum)
The first argument represents the parent component for the JOptionPane for when the
ProgressMonitor needs to appear. The parent component is the component over which the
pop-up window appears, and acts like the parentComponent argument for the createDialog()
method of JOptionPane . You then provide the static and variable message parts for the message
area of the JOptionPane . Either of these message parts could be null , although null means that
this part of the message area will never appear. Lastly, you provide minimum and maximum values
as the range for the progress bar. The difference between these two values represents the expected
number of operations to be performed, such as the number of files to load or the size of a file to
read. Normally, the minimum setting is zero, but that isn't required. The number of completed
operations determines how far the progress bar moves.
Initially, the pop-up window isn't displayed. By default, the progress monitor checks every
half second (500 milliseconds) to see if the task at hand will complete in two seconds. If the task
has shown some progress and it still won't complete in two seconds, then the pop-up window
appears. The time to completion is configurable by changing the millisToDecideToPopup and
millisToPopup properties of the ProgressMonitor .
The following line of source demonstrates the creation of a ProgressMonitor with 200 steps
in the operation. A reference to the ProgressMonitor would need to be saved so that it can be
notified as the task progresses.
Search WWH ::




Custom Search