如何在非事件分派线程中间提示确认对话框 [英] How to prompt a confirmation dialog box in the middle of non event dispatching thread
本文介绍了如何在非事件分派线程中间提示确认对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下fun
,将由非事件分派线程执行.在线程中间,我想要一个
I have the following fun
which will be executed by non event dispatching thread. In the middle of thread, I want a
- 弹出确认框.线程暂停其执行.
- 用户做出选择.
- 线程将获得选择并继续执行.
但是,我发现以线程安全方式进行操作并不容易,因为对话框应该由事件分派线程显示.我尝试
However, I find out it is not easy to do it in thread safety way, as dialog box should be shown by event dispatching thread. I try
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
是最终的,而且我无法将对话框返回的值分配给它.
Of course this won't work as choice
is final, and I cannot assign the returned value from dialog to it.
实现以上三个目标的正确方法是什么?
What is the correct way to achieve the above 3 objectives?
推荐答案
您是否尝试过:
public int fun()
{
// The following code will be executed by non event dispatching thread.
final int[] choice = new int[1];
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
// Error.
choice[0] = JOptionPane.showConfirmDialog(SaveToCloudJDialog.this, message, title, JOptionPane.YES_NO_OPTION);
}
});
return choice[0];
}
这篇关于如何在非事件分派线程中间提示确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文