SwingWorker中的取消异常

时间:2014-03-20 10:04:12

标签: java swing swingworker

我试图通过单击JFrame表单上的按钮来取消功能。

这是我的工作人员:

private void start(final Callable<Void> func) {
        worker = new SwingWorker<Boolean, Integer>() {
            @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<Integer> chunks) {
                progressBar.setStringPainted(true);
                progressBar.setIndeterminate(true);

            }

        };

这是我试图取消SwingWorker对象:

   dialog.addWindowListener(new WindowAdapter() 
{

  @Override
  public void windowClosing(WindowEvent e)
  {

    worker.cancel(true);

  }
});

但是当我点击关闭按钮时,它会给我异常,并且func方法中的doinBackground()未被取消。我该如何停止func方法?

1 个答案:

答案 0 :(得分:2)

在对CallableCallableStatement接口的可怜的误解之后(很抱歉,我必须离开JDBC一段时间:P)我已经做出的观点仍然有效(幸运的是)。您可以在SwingWorker#isCancelled()实施中使用done()方法,如下所示:

@Override
protected void done() {
    if(isCancelled()) {
        try {
            func.cancel(); // you need to provide this method
            dialog.dispose();
        } catch(InterruptedException ex) {
             progressBar.setString("ERROR");
             Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

当然,您需要为可调用的实现提供cancel()方法,这可能很容易或很难,具体取决于call()方法实际执行的操作。例如:

abstract class AbstractCancellableTask<T> implements Callable<T> {

    public abstract void cancel() throws InterruptedException;
}

实施例

我使用Callable更新了我之前的示例。请注意,在调用worker.cancel(true)方法时,任务将被取消。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Demo {

    private void createAndShowGUI() {

        final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

            private AbstractCancellableTask<Void> myCallable;

            @Override
            protected Void doInBackground() throws Exception {

                myCallable = new AbstractCancellableTask<Void>() {

                    private volatile boolean flag = true;

                    @Override
                    public void cancel() throws InterruptedException {
                        flag = false;
                    }

                    @Override
                    public Void call() throws Exception {
                        while(flag) {
                            System.out.println("Running heavy task...");

                        }
                        System.out.println("Stopped!");
                        return null;
                    }
                };

                myCallable.call();
                return null;
            }

            @Override
            protected void done() {
                if(isCancelled()) {
                    try {
                        myCallable.cancel();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        };

        JToggleButton button = new JToggleButton("Start...", false);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JToggleButton toggleButton = (JToggleButton)e.getSource();
                if(toggleButton.isSelected()) {
                    worker.execute();
                    toggleButton.setText("Stop it!");
                } else {
                    worker.cancel(true);
                    toggleButton.setEnabled(false);
                }
            }
        });

        JPanel content = new JPanel();
        content.add(button);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGUI();
            }
        });
    }

    abstract class AbstractCancellableTask<T> implements Callable<T> {

        public abstract void cancel() throws InterruptedException;
    }
}