title: Cancellation Exception in SwingWorker
body: <p>I'm trying to cancel a function from a button click on a JFrame form.</p>
  
  <p>Here is my worker function:</p>
  
  <pre><code>private void start(final Callable&lt;Void&gt; func) {
          worker = new SwingWorker&lt;Boolean, Integer&gt;() {
              @Override
              protected Boolean doInBackground() throws Exception {
                  publish(2);
                  func.call();
                  return true;
              }
  
              @Override
              protected void done() {
                  boolean status;
                  try {
                      status = get();
                      dialog.dispose();
                  } catch (InterruptedException | ExecutionException e) {
                      progressBar.setString("ERROR");
                      Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, e);
                  }
              }
  
              @Override
              protected void process(List&lt;Integer&gt; chunks) {
                  progressBar.setStringPainted(true);
                  progressBar.setIndeterminate(true);
  
              }
  
          };
  </code></pre>
  
  <p>This is what i tried to cancel the SwingWorker object:</p>
  
  <pre><code>   dialog.addWindowListener(new WindowAdapter() 
  {
  
    @Override
    public void windowClosing(WindowEvent e)
    {
  
      worker.cancel(true);
  
    }
  });
  </code></pre>
  
  <p>But when i click on close button, it gives me exception and <code>func</code> is not cancelled inside <code>doinBackground()</code> method. How can i stop <code>func</code> method?</p>