Monitoring Progress

By themselves, progress bars are pretty boring. Swing, however, combines progress bars with the dialog capabilities of JOptionPane to create the ProgressMonitor and ProgressMonitorInputStream classes. You can use ProgressMonitor to report on the current progress of a potentially long task. You can use ProgressMonitorInputStream to automatically monitor the amount of data that has been read in with an InputStream. With both, you can define various strings to be posted in the progress monitor dialogs to offer a better explanation of the task at hand.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ProgressMonitorExample extends JFrame implements ActionListener {

	static ProgressMonitor pbar;
	static int counter = 0;

	public ProgressMonitorExample() {
		super("Progress Monitor Demo");
		setSize(250, 100);

		pbar = new ProgressMonitor(null, "Monitoring Progress",
				"Initializing . . .", 0, 100);

		// Fire a timer every once in a while to update the progress.
		Timer timer = new Timer(500, this);

	public static void main(String args[]) {
		UIManager.put("ProgressMonitor.progressText", "This is progress?");
		UIManager.put("OptionPane.cancelButtonText", "Go Away");
		new ProgressMonitorExample();

	public void actionPerformed(ActionEvent e) {
		// Invoked by the timer every 0.5 seconds. Simply place
		// the progress monitor update on the event queue.
		SwingUtilities.invokeLater(new Update());

	class Update implements Runnable {
		public void run() {
			if (pbar.isCanceled()) {
			pbar.setNote("Operation is " + counter + "% complete");
			counter += 2;