我有以下fun
将由非事件调度线程执行。在线程的中间,我想要一个
- 弹出一个确认框。线程暂停其执行。
- 用户做出选择。
- 线程将获得选择并继续执行。
但是,我发现以线程安全的方式做到这一点并不容易,因为对话框应该由事件调度线程显示。我试试
public int fun()
{
// The following code will be executed by non event dispatching thread.
final int choice;
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
// Error.
choice = JOptionPane.showConfirmDialog(SaveToCloudJDialog.this, message, title, JOptionPane.YES_NO_OPTION);
}
});
return choice;
}
当然,这不会像choice
最终一样工作,我无法将对话框的返回值分配给它。
实现上述三个目标的正确方法是什么?