How To Create A Progress Monitor In Swing

Introduction

The ProgressMonitor class is used to monitor the progress of some operation. When a user initiated operation takes a significant amount of time, visual feedback should be provided to the user. A common means for this visual feedback is by displaying the wait cursor. When ProgressMonitor is created, it is given in a numeric range with a descriptive string.

How ProgressMonitor Class Works?

As the operation progresses, it calls the setProgress() method to indicate how far the operation is along the [min, max] range.

The ProgressMonitor class will close the dialog box when the progress is set to the maximum value of the operation range. The operation can also use the close method to communicate to the ProgressMonitor that the operation is complete and the dialog box should be closed and disposed of. A ProgressMonitor object can be instantiated by calling the constructor as

ProgressMonitor(Component parentcomponent, object message , String note, int max, int min) 

Construct a graphic object that shows progress, typically by filling in a rectangular bar as the process nears completion.

The ProgressMonitor is created with an operation range of 0-100. A multiline message is specified. A note is given in the constructor because the sample operation will update the note as it progresses. The ProgressMonitor does not immediately show the dialog box. It waits for the progress to be set by the operation and then estimates the duration of the operation. The dialog box will be displayed if this time is longer than the operation during the threshold.

Consider a problem to create a single button in a program monitor that is shared between all operations. Thus, multiple threads will update the program monitor if the start button is clicked when the program meter is displayed.

Source Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ProgressTest extends Object {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Progress Bar/Monitor Example");
        JButton button = new JButton("Start");
        frame.getContentPane().add(button, BorderLayout.CENTER);
        int min = 0;
        int max = 100;
        String[] message = new String[2];
        message[0] = "Performing Operation";
        message[1] = "This may take some times........";
        final ProgressMonitor monitor = new ProgressMonitor(frame, message, "Iteration", min, max);
        final Runnable runnable = new Runnable() {
            public void run() {
                int sleepTime = 500;
                for (int i = 1; i < 100; i++) {
                    try {
                        monitor.setNote("Iteration" + i);
                        monitor.setProgress(i);
                        if (monitor.isCanceled()) {
                            monitor.setProgress(100);
                            break;
                        } //if close
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException dontcare) {}
                } //close for loop
                monitor.close();
            } //close run()
        };
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Thread thread = new Thread(runnable);
                thread.start();
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}

Output 

How To Create A Progress Monitor In Swing

How To Create A Progress Monitor In Swing

Implementation of Code

The setProgress method is used to update the progress of the operation. The progress is an integer in the current operating range. The operation range is specified in the ProgressMonitor’s constructor and can be altered with the setMinimum and setMaximum methods. The get versions of each of these methods can be used to determine the current operating range. In the setProgress method, the ProgressMonitor determines if the dialog box is required. This class will work best if the operation can supply consistent calls to the setProgress method.

As the operation updates the progress, it can also update the String note presented to the user using the setNote method. For instance, if the operation is processing a list of customer’s details, the name of the customer being operated on could be presented in the note. A non-null note must be specified before the dialog box is created when the operation is going to update the note during the operation. If the note is null when the dialog box is created, a note will not be included in the dialog box, and subsequent calls to the setNote will be ignored.

Summary

The ProgressMonitor class will close the dialog box when the progress is set to the maximum value of the operation range. The ProgressMonitor does not immediately show the dialog box. It waits for the progress to be set by the operation and then estimates the duration of the operation.