public boolean startProgressMonitorTask( ProgressMonitorTask pmt) {
    if (busy) return false;
    busy = true;

    this.task = pmt;
    isCancelled = false;
    count = 0;
    setIcon(icon[0]);

    // create timer, whose events happen on the awt event Thread
    ActionListener watcher = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        //System.out.println("timer event"+evt);

        if (isCancelled && !task.isCancel()) {
          task.cancel();
          if (debug) System.out.println(" task.cancel");
          return; // give it a chance to finish up
        } else {
          // indicate progress
          count++;
          setIcon( icon[count % 2]);
          if (debug) System.out.println(" stop count="+count);
        }

        // need to make sure task acknowledges the cancel; so dont shut down
        // until the task is done
        if (task.isDone()) {
          if (myTimer != null)
            myTimer.stop();
          myTimer = null;

          if (task.isError())
            javax.swing.JOptionPane.showMessageDialog(null, task.getErrorMessage());

          if (task.isSuccess())
            fireEvent( new ActionEvent(this, 0, "success"));
          else if (task.isError())
            fireEvent( new ActionEvent(this, 0, "error"));
          else if (task.isCancel())
            fireEvent( new ActionEvent(this, 0, "cancel"));
          else
            fireEvent( new ActionEvent(this, 0, "done"));

          busy = false;
        }
      }
    };
    myTimer = new javax.swing.Timer(1000, watcher); // every second
    myTimer.start();

    // do task in a seperate, non-event, thread
    Thread taskThread = new Thread(task);
    taskThread.start();

    return true;
  }
public boolean startProgressMonitorTask ( ProgressMonitorTask pmt ) { if ( busy ) return false ; busy = true ; this . task = pmt ; isCancelled = false ; count = 0 ; setIcon ( icon [ 0 ] ) ; ActionListener watcher = new ActionListener ( ) { public void actionPerformed ( ActionEvent evt ) { if ( isCancelled && ! task . isCancel ( ) ) { task . cancel ( ) ; if ( debug ) System . out . println ( " task.cancel" ) ; return ; } else { count ++ ; setIcon ( icon [ count % 2 ] ) ; if ( debug ) System . out . println ( " stop count=" + count ) ; } if ( task . isDone ( ) ) { if ( myTimer != null ) myTimer . stop ( ) ; myTimer = null ; if ( task . isError ( ) ) javax . swing . JOptionPane . showMessageDialog ( null , task . getErrorMessage ( ) ) ; if ( task . isSuccess ( ) ) fireEvent ( new ActionEvent ( this , 0 , "success" ) ) ; else if ( task . isError ( ) ) fireEvent ( new ActionEvent ( this , 0 , "error" ) ) ; else if ( task . isCancel ( ) ) fireEvent ( new ActionEvent ( this , 0 , "cancel" ) ) ; else fireEvent ( new ActionEvent ( this , 0 , "done" ) ) ; busy = false ; } } } ; myTimer = new javax . swing . Timer ( 1000 , watcher ) ; myTimer . start ( ) ; Thread taskThread = new Thread ( task ) ; taskThread . start ( ) ; return true ; }
The given task is run in a background thread.
Progress is indicated once a second.
You cannot call this method again till the task is completed.
@param pmt heres where the work is done.
@return true task was started, false if still busy with previous task.