ProgressMonitor

Question:
How do I use the ProgressMonitor class?

Answer:
The ProgressMonitor class is a convenience class that saves you fromthe hassle of creating your own dialog with a JProgressBar. It willpop up a dialog containing a JProgressBar that displays theprogress of an operation.

ProgressMonitor will only pop up themonitor after a set minimum amount of time has passed. The default valueis 500 milliseconds and can be set with setMillisToDecideToPopup(int). Based on the progress ofthe operation up to this point, ProgressMonitor will decide how muchlonger it will take for the operation to complete. Only if this time isgreater than a given threshold will the dialog be displayed. Thedefault threshold is 2 seconds and can be set withsetMillisToPopup).

The progress of an operation is conveyed to the ProgressMonitor bycalling setProgress(int) with an integer argumentrepresenting the progress. The ProgressMonitor constructor accepts aminimum value and a maximum value for the progress indicator in orderto determine when and how to update the progress bar.

If you are not careful, you can use ProgressMonitor to no effect. Themost common cause is when your operation completes too quickly. Youcan get around this by reducing the popup threshold values to zero.Another situation that can occur is that the progress dialog willappear, but no updates will occur. You can prevent this by notcalling setProgress so often that the AWT threaddoesn’t get a chance to update the progress bar.

In the end, I amnot too keen on the ProgressMonitor class. The dialog it creates hasboth an OK and a Cancel button. The Cancel button makes sense andis intended to terminate the operation. Before calling setProgress, you should test if the operation has beencanceled with boolean isCanceled(), which will returntrue if the cancel button has been pressed. The OK button doesn’tmake much sense to me, and will make the dialog disappear even beforethe operation has completed. Right now I consider this a bug anddon’t recommend using ProgressMonitor because the OK button canconfuse users.

The following example code shows how you might use the ProgressMonitorclass. I use a dummy loop to simulate work, but you could imaginethat bytes were being read from a socket during a file download.

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Progress {  public static final int LOOP_MAX = 15000;  public static void main(String[] args) {    JFrame frame = new JFrame("Progress Demo");    ProgressMonitor progress;    WindowListener exitListener;    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {        Window window = e.getWindow();        window.setVisible(false);        window.dispose();        System.exit(0);      }    };    frame.addWindowListener(exitListener);    frame.setSize(400, 400);    frame.setVisible(true);    progress =      new ProgressMonitor(frame, "Executing dummy loop.",			  "Iterating.", 0, LOOP_MAX);    for(int i = 0; i <= LOOP_MAX; ++i) {      // Simulate some work      for(int j = 0; j <= LOOP_MAX; ++j);      if(progress.isCanceled())	break;      progress.setProgress(i);    }  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: