Class ProgressMonitor


  • public class ProgressMonitor
    extends Object
    This wraps a javax.swing.ProgressMonitor, which allows tasks to be canceled. This class adds extra behavior to javax.swing.ProgressMonitor:
    1. Pass in the ProgressMonitorTask you want to monitor.
    2. Throws an actionEvent (on the AWT event thread) when the task is done.
    3. If an error, pops up an error message.
    4. Get status: success/failed/cancel when task is done.

    The ProgressMonitorTask is run in a background thread while a javax.swing.ProgressMonitor dialog box shows progress. The task is checked every second to see if its done or canceled.

     Example:
    
     AddDatasetTask task = new AddDatasetTask(datasets);
     ProgressMonitor pm = new ProgressMonitor(task);
     pm.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       if (e.getActionCommand().equals("success")) {
         doGoodStuff();
       }
      }
     });
     pm.start( this, "Add Datasets", datasets.size());
    
     (or)
    
     AddDatasetTask task = new AddDatasetTask(datasets);
     ProgressMonitor pm = new ProgressMonitor(task, () -> doGoodStuff());
     pm.start( this, "Add Datasets", datasets.size());
     
    • Method Detail

      • addActionListener

        public void addActionListener​(ActionListener l)
        Add listener: action event sent when task is done. event.getActionCommand() =
        • "success"
        • "error"
        • "cancel"
        • "done" if done, but success/error/cancel not set
      • removeActionListener

        public void removeActionListener​(ActionListener l)
      • start

        public void start​(Component top,
                          String taskName,
                          int progressMaxCount)
        Call this from awt event thread. The task is run in a background thread.
        Parameters:
        top - put ProgressMonitor on top of this component (may be null)
        taskName - display name of task
        progressMaxCount - maximum number of Progress indicator