ProgressMonitor Class : ProgressMonitor « Swing « Java Tutorial






The ProgressMonitor class is used to report on the status of a time-consuming task.

Creating a ProgressMonitor

public ProgressMonitor(Component parentComponent, Object message, String note, int minimum, int maximum)
ProgressMonitor monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
monitor.setNote("Loaded " + progress + " files");

The millisToDecideToPopup property represents the number of milliseconds that the monitor waits before deciding if it needs to display the pop-up window.

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

public class SampleProgress {
  static ProgressMonitor monitor;

  static int progress;

  static Timer timer;

  public static void main(String args[]) {
    JFrame frame = new JFrame("ProgressMonitor Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(0, 1));

    JButton startButton = new JButton("Start");
    ActionListener startActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Component parent = (Component) actionEvent.getSource();
        monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
        progress = 0;
      }
    };
    startButton.addActionListener(startActionListener);
    frame.add(startButton);

    JButton increaseButton = new JButton("Manual Increase");
    ActionListener increaseActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        if (monitor == null)
          return;
        if (monitor.isCanceled()) {
          System.out.println("Monitor canceled");
        } else {
          progress += 5;
          monitor.setProgress(progress);
          monitor.setNote("Loaded " + progress + " files");
        }
      }
    };
    increaseButton.addActionListener(increaseActionListener);
    frame.add(increaseButton);

    JButton autoIncreaseButton = new JButton("Automatic Increase");
    ActionListener autoIncreaseActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        if (monitor != null) {
          if (timer == null) {
            timer = new Timer(250, new ActionListener() {

              public void actionPerformed(ActionEvent e) {
                if (monitor == null)
                  return;
                if (monitor.isCanceled()) {
                  System.out.println("Monitor canceled");
                  timer.stop();
                } else {
                  progress += 3;
                  monitor.setProgress(progress);
                  monitor.setNote("Loaded " + progress + " files");
                }
              }
            });
          }
          timer.start();
        }
      }
    };
    autoIncreaseButton.addActionListener(autoIncreaseActionListener);
    frame.add(autoIncreaseButton);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}








14.110.ProgressMonitor
14.110.1.ProgressMonitor ClassProgressMonitor Class
14.110.2.Creating a Progress Monitor Dialog
14.110.3.Create a ProgressMonitor toolbar
14.110.4.Implement ProgressMonitor toolbar with a file to monitor
14.110.5.Customizing a ProgressMonitor Look and Feel
14.110.6.Setting the Popup Delay of a Progress Monitor Dialog
14.110.7.JAR Archives: Pack Progress Monitor