Java swing workflow wait EDT

I have a workflow that needs to wait for EDT to update the GUI before proceeding. I used the publish method to tell EDT to change something. How can I make a worker wait for this change to happen?

+5
source share
2 answers

If this is also your workflow that initiates changes to the GUI, then there is a ready-made mechanism for waiting for these changes:

SwingUtilities.invokeAndWait ()

should fit the bill well.

SwingUtilities.invokeLater(), EDT , , EDT , .. . invokeLater(), a wait(), , EDI , wait(). .

+4

, SwingWorker . , , . , . , .

class MyWorker extends SwingWorker<K,V>
{
    boolean processed = true;

    protected void doInBackground() {
        for (;;) {
            setProcessed(false);
            publish(results);
            waitProcessed(true);
        }
    }

    synchronized void waitProcessed(boolean b) {
        while (processed!=b) {
           wait();
        }
        // catch interrupted exception etc.
    }

    synchronized void setProcessed(boolean b) {
        processed = b;
        notifyAll();
    }


    protected void process(List<V> results) {
       doStuff();
       setProcessed(true);
    }
}
+5

All Articles