I thought I understood EDQ until I hit this problem. I have the code shown below. It reads from a Bufferred reader. If the first character received is a “Z” I execute one set of code (displaying a JOptionPane) and if it is a 0 I execute another section of code (displaying another JOptionPane). I am trying to do this within the EDQ and so I use SwingUtilities invokeAndWait. When I test these error conditions, the first statement in the conditional works as designed, but I get a java error when testing the else clause. Specifically:
Exception in thread “AWT-EventQueue-2” java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(Unknown Source)
It is all part of the same conditional. How can one clause be part of the EDQ and another clause not be.
This is crazy.
Thanks for any help.
Elliott
while ((line = in.readLine()) != null) {
if (line.charAt(0) == 'Z') {
String theMsg;
theMsg = "No records were found.";
try {
SwingUtilities.invokeAndWait(new DoShowDialog(null, theMsg, 0));
} catch (java.lang.reflect.InvocationTargetException e1) {
e1.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
noDocs = true;
Object args[] = new Object[1];
args[0] = "1";
window.call("uploadConfig", args);
downloadAccount.setEnabled(true);
uploadAccount.setEnabled(false);
deleteAllUnselectedCodes.setEnabled(false);
queue = null;
if (poll) {
polltimer.restart();
}
} else if (line.charAt(0) == 'O') {
String theMsg;
theMsg = "Account is currently checked out
by user "+ line.substring(1)
+ ". You can view this
account but you cannot modify it. ";
try {
SwingUtilities.invokeAndWait(new DoShowDialog(null, theMsg, 0));
} catch (java.lang.reflect.InvocationTargetException e1) {
e1.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
initialckBoxState = false;
accountfoundandnotcheckedout = true;
viewMode = true;
patientpane.setEditFields(false);
otherpane.setEditFields(false);
getAccountImages(acctEntered); // bluenoble
}
.....................
more stuff
}
Execution doesn’t jump threads.
Thus all the code posted runs in the EDT (Event Dispatch Thread) and it refuses to
invokeAndWaitbecause that causes an inherent deadlock. (Actually, it could be turned into an invoke-immediate as done with SynchronizationContexts in .NET, but it was not designed as such.)In this particular case I suspect the code is invoked from within an EDT callback (and copy’n’pasted from another scenario). The “trivial fix” (that would eliminate this exception) would be to eliminate the
invokeAndWaitmethods, but that will have a negative impact if this code is invoked off the EDT as well — the two situations much be handled differently. Take some time to determine when/where this code will run, and why.As others have pointed out, this code seems confused: if it’s off the EDT, manipulating Swing objects is bad, and if it’s on the EDT then there is no need to
invokeAndWaitand blocking is bad.Happy coding.