I have a SwingWorker thread that launches a modal dialog box (from a property change listener that listens to the StateValue of started) and the swing worker proceeds to do its work. However, it looks like the done method is not called because that is called on the EDT but the swing worker's modal dialog is blocking the EDT. So, I can't close the dialog from the EDT (or from the done method). Right now I'm just closing the dialog from the doInBackground at the end of that method, but that seems a little unsafe from the doInBackground since it's not on the EDT. What's the best way to handle this? thanks.
+1
A:
The dispatch loop should continue to dispatch the events associated with SwingWorker
even when a modal dialog is displayed.
This works for me.
import javax.swing.*;
public class Unions {
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
runEDT();
}});
}
private static void runEDT() {
final JDialog dialog = new JDialog((JFrame)null, true);
new SwingWorker<Void,Void>() {
@Override protected Void doInBackground() throws Exception {
// But this is working.
Thread.sleep(3000);
return null;
}
@Override protected void done() {
dialog.setVisible(false);
}
}.execute();
dialog.setVisible(true);
}
}
Tom Hawtin - tackline
2009-12-18 01:24:19
I was accidentally showing the dialog from a non-EDT thread and so the problems only occurred sometimes. I'm guessing there was some issue with that (though I can't figure out exactly where the problem was being caused), but all is good now, thanks.
Jeff Storey
2009-12-18 01:40:07
`assert java.awt.EvenQueue.isDispatchThread();` is your friend (and `assert !java.awt.EvenQueue.isDispatchThread();`).
Tom Hawtin - tackline
2009-12-18 02:05:30