我正在使用 SwingWorker 在后台执行某些操作。在执行过程中,我需要通过抛出 JoptionPane.showOptionDialog() 来询问用户一些事情。我不想在我的模型类中执行此操作,也不想在 SwingWorker.doInBackground 执行时执行此操作。
相信很多人都遇到过这种情况。
所以我必须从对 doInBackground 的调用返回,然后在 done() 中请求用户输入。然后我需要启动另一个 SwingWorker 并从 done 方法执行 doInBackground() 吗?
还有另一种更简洁/更简单的方法吗?
更新(针对 mkorbel 的问题)
类设计是这样的:
public class OptionInSwingWorker {
public static void main(String[] args) {
JFrame frame=new JFrame();
JButton test = new JButton("Test");
frame.add(test);
test.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void,Void>(){
@Override
protected Void doInBackground() throws Exception {
// check for a value in the database
// if value is something.. throw up an OptionPane
// and ask the user a question..
// then do something...
return null;
}
@Override
protected void done() {
// open some other dialog
}
}.execute();
}
});
}
}