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;
}
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?
Have you tried:
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];
}