title: How to delegate SwingWorker's publish to other methods
body: <p>My "problem" can be described by the following. Assume we have an intensive process that we want to have running in the background and have it update a Swing JProgress bar. The solution is easy:</p>
  
  <pre><code>import java.util.List;
  
  import javax.swing.JOptionPane;
  import javax.swing.JProgressBar;
  import javax.swing.SwingWorker;
  
  
  /**
   * @author Savvas Dalkitsis
   */
  public class Test {
  
      public static void main(String[] args) {
          final JProgressBar progressBar = new JProgressBar(0,99);
          SwingWorker&lt;Void, Integer&gt; w = new SwingWorker&lt;Void, Integer&gt;(){
  
              @Override
              protected void process(List&lt;Integer&gt; chunks) {
                  progressBar.setValue(chunks.get(chunks.size()-1));
              }
  
              @Override
              protected Void doInBackground() throws Exception {
  
                  for (int i=0;i&lt;100;i++) {
                      publish(i);
                      Thread.sleep(300);
                  }
  
                  return null;
              }
  
          };
          w.execute();
          JOptionPane.showOptionDialog(null,
                  new Object[] { "Process", progressBar }, "Process",
                  JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                  null, null, null);
      }
  
  }
  </code></pre>
  
  <p>Now assume that i have various methods that take a long time. For instance we have a method that downloads a file from a server. Or another that uploads to a server. Or anything really. What is the proper way of delegating the publish method to those methods so that they can update the GUI appropriately?</p>
  
  <p>What i have found so far is this (assume that the method "aMethod" resides in some other package for instance):</p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.util.List;
  
  import javax.swing.AbstractAction;
  import javax.swing.Action;
  import javax.swing.JOptionPane;
  import javax.swing.JProgressBar;
  import javax.swing.SwingWorker;
  
  
  /**
   * @author Savvas Dalkitsis
   */
  public class Test {
  
      public static void main(String[] args) {
          final JProgressBar progressBar = new JProgressBar(0,99);
          SwingWorker&lt;Void, Integer&gt; w = new SwingWorker&lt;Void, Integer&gt;(){
  
              @Override
              protected void process(List&lt;Integer&gt; chunks) {
                  progressBar.setValue(chunks.get(chunks.size()-1));
              }
  
              @SuppressWarnings("serial")
              @Override
              protected Void doInBackground() throws Exception {
  
                  aMethod(new AbstractAction() {
  
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          publish((Integer)getValue("progress"));
                      }
                  });
  
                  return null;
              }
  
          };
          w.execute();
          JOptionPane.showOptionDialog(null,
                  new Object[] { "Process", progressBar }, "Process",
                  JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                  null, null, null);
      }
  
      public static void aMethod (Action action) {
          for (int i=0;i&lt;100;i++) {
              action.putValue("progress", i);
              action.actionPerformed(null);
              try {
                  Thread.sleep(300);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  </code></pre>
  
  <p>It works but i know it lacks something. Any thoughts?</p>