我正在使用SwingWorker在后台执行某些操作。在执行过程中,我有一个条件,我需要通过抛出一个JoptionPane.showOptionDialog()来询问用户。 我不想在我的模型类中做到这一点,并且不想在执行SwingWorker.doInBackground时执行此操作。执行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();
}
});
}
}
我用什么来传递窗口的引用,以及我用于调用JOption的工作线程的进度方法。但它不是一个好主意,如果处理不当,最终会出现很多弹出窗口。使用超时关闭一些默认值 –
在执行过程中,我有一个...,是从嵌套的无效或由SwingWorker执行的类谈论异常 – mKorbel