![]() ![]() ![]() |
23
Progress Helper
Features of JCProgressHelper
Constructors and Associated Classes
JCProgressHelper Methods
Examples
23.1 Features of JCProgressHelper
JCProgressHelper
is a class that lets you create and manage a thread-safe progress dialog. With it, you can monitor some potentially time-consuming operation and present a visual indication of its progress. If it looks like the operation will take some time, a progress dialog appears. Before the operation is started theJCProgressHelper
should be given a numeric range and a descriptive String. Initially, there is noJProgressBar
. As the operation progresses, call theupdateProgress()
method to indicate how far along the [min .. max] range the operation is. After the firsttimeToDecideToPopup
milliseconds (default 500) the progress monitor will predict how long the operation will take. If it is longer thantimeToPopup
(default 2 seconds) aJProgressBar
is popped up.
Figure 56 : A JCProgressHelper showing the methods used for labelling.
The advantages of
JCProgressHelper
include:
- You are able to quantify the process that the
JCProgressHelper
is monitoring by setting two integers representing a minimum and a maximum of some value that proportionately measures the progress of some time-consuming operation. As the process continues, you callupdateProgress()
with a parameter indicating how far along things are.- The progress helper transforms all your calls to it into Thread-safe calls to the parent Swing component to encourage frequent updating.
- A descriptive, dynamically updatable, message informs users about the progress of the operation.
- The progress dialog contains a Cancel button, permitting the end-user to terminate long-running processes.
- The progress dialog waits for a time that you set in
setTimeToDecideToPopup()
before checking whether to pop up, and does not pop up at all unless the operation is projected to take at least a minimum time, which you may set also, insetPopupTime()
.
23.2 Constructors and Associated Classes
23.2.1 Constructors
JCProgressHelper(Component parent)
JCProgressHelper(Component parent, String static_message, int min, int max)
JCProgressHelper(Component parent, String static_message, int min, int max, boolean show_dynamic_message, boolean is_modal, boolean is_dismissable)
The parameters in the constructors are:
23.2.2 Associated Classes
Following is a list of the
JCProgressHelper
associated classes:
23.2.3 Using the Event and Listener Classes
Depending on your needs, there are four ways to use the progress mechanism:
- Use the
JCProgressHelper
GUI and let it handle all updates without any need to invoke events and listeners in your code. In this case, you callstartProgress()
andupdateProgress()
to control the progress bar. Seeexamples.elements.BasicProgressHelperExample.java in the JCLASS_HOME/examples/elements directory.
- Use
createProgressListener()
to haveJCProgressHelper
manage events internally. In this case, you call listener methods likeprocessingBegin()
andprocessingUnit()
, which are implemented by the progress meter itself. You don't have to supply the code.- Create your own
addProgressListener()
method and have it registerJCProgressListener
s. This option gives you the most control. You'll need to have one of your classes implementJCProgressListener
, or you can extendJCProgressAdapter
. You override the listener methods you need, and you will have to provide implementations ofaddProgressListener(JCProgressListener)
,removeProgressListener(JCProgressListener)
, andfireProgressEvent(JCProgressEvent)
in the class that wishes to control the progress meter. See ProgressListenerExample.java in the JCLASS_HOME/examples/elements directory.- Use an implementation of
JCProgressCancelledListener
to handle what must be done if the end user clicks theJCProgressHelper
's Cancel button. AJCProgressCancelledEvent
is sent to allJCProgressCancelledListener
s when this button is clicked. If you use this listener your application is notified immediately when the Cancel button has been clicked, rather than having to wait untilprocessingUnit()
is called fromJCProgressListener
.
23.3 JCProgressHelper Methods
23.4 Examples
Add a
JCProgressHelper jpr = new JCProgressHelper(eFrame, "Here is a progress message", 0, 10, true, true, true);JCProgressHelper
to your component as follows:Set a time which it is unnecessary to display a progress meter and call
setPopupTime()
with this value. Decide when you want the progress helper to calculate its estimation of the monitored process' completion time and callsetTimeToDecideToPopup()
with this time. This time should be long enough that the tracking variable is no longer at its minimum value, so that the progress meter has a way of estimating how long the operation will take to complete.Call the
jpr.updateProgress(5); // Progress indicator is half-way alongupdateProgress()
method periodically to update the tracking variable:When the process has completed, call
completeProgress()
to remove the dialog.A full example
The following code causes a progress dialog to appear. It sets both a static message and a dynamic message that is changed every time the progress bar is updated. Since the dialog is managed in an AWT thread, you may have to make sure that it is given a chance to run, especially if you wish to perform some initialization of the progress dialog in the mainline thread and you wish it to appear in the dialog.
import javax.swing.*;
import com.klg.jclass.util.swing.JCProgressHelper;
public class ProgressHelper extends JFrame {
// Use the constructor that permits setting a static message
public ProgressHelper() {
JCProgressHelper jpr = new JCProgressHelper(this, "Here is a static progress message", 0, 100, true, true, false);
// Pop up the progress dialog.
// There will be a delay, determined in part
// by the two popup time parameters.
jpr.startProgress();
for (int j = 1; j < 11; j++){
// Simulate an ongoing process ...
try {
Thread.sleep(1000);
} catch (Exception e) {
}
// ... and update the progress meter periodically,
jpr.updateProgress(j*10);
// changing the dynamic message as the meter updates.
jpr.setDynamicMessage("Dynamic "+ j*10);
}
// Dispose of the progress meter
jpr.completeProgress();
// The mainline program can continue as required...
// ... until its tasks are completed.
System.exit(0);
}
public static void main(String[] args) {
ProgressHelper ph = new ProgressHelper();
}
}
![]() ![]() ![]() |