You are trying something conceptionally wrong. You try to wait for a completion to perform another action which yields to the single-threaded behavior that you want to avoid. It doesn’t matter how you rewrite your code, as long as you always end up trying to wait your GUI will freeze.
You have to change your concept. Instead of waiting you have to tell Swing what to do when the task has finished, i.e. start the next one. You don’t need to invoke get
as it has nothing to do with defining the next action. You can either:
Tell your
ProgressBarSwingWorker
the reference to the next one and invoke itsexecute
method inside thedone
methodclass ProgressBarSwingWorker extends SwingWorker<Object, Object> { ProgressBarSwingWorker next; // … public ProgressBarSwingWorker(ProgressBarSwingWorker nextWorker) { next=nextWorker; } // … @Override protected void done() { if(next!=null) next.execute(); } }
or
add a listener which will invoke the
execute
method of the nextProgressBarSwingWorker
ProgressBarSwingWorker previous=null; for(final ProgressBarSwingWorker p: progressList) { if(previous==null) p.execute(); else previous.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if("state".equals(evt.getPropertyName()) && evt.getNewValue() == SwingWorker.StateValue.DONE) p.execute(); } }); previous=p; }