SwingWorker - How not to block EDT
843807Jul 15 2010 — edited Jul 16 2010In our Swing app, we are using SwingWorker for each Server side calls that makes a Database request and returns the rowset.
What I am noticing is even though the UI is not greyed out while its making a server call, its not letting me use other parts of the app like clicking on the menu etc.
This is how the calls looks. I am calling get() after the start() method, but only after checking is the worker is done().
This should not block the EDT thread right? Please let me know if its alright.
public Object doAsynServletCommunication(RequestMap requestMap, URL servletUrl, String sMessage, Frame frame, boolean bCancelEnabled) throws Exception {
TestSwingWorker worker = new TestSwingWorker (this, requestMap, servletUrl);
worker.start();
if(worker.isDone()) {
try {
return worker.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// If not an event Thread, so synchronized communication
return doServletCommunication(requestMap, servletUrl);
}