Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - execute each SwingWorker class one after another

I have multiple classes extending the SwingWorker. What I wish to accomplish is to execute each class one after another (without executing the next class in the previous class' done method). For example, lets say I have:

ClassSwingW1 csw1 = new ClassSwingW1();
csw1.execute;

ClassSwingW2 csw2 = new ClassSwingW2();
csw2.execute;

ClassSwingW3 csw3 = new ClassSwingW3();
csw3.execute;

and etc.

public class ClassSwingW1 extends SwingWorker<Void, Void> {

    @Override
    protected Void doInBackground() throws Exception {
        //do something

        return null;
    }

}

public class ClassSwingW2 extends SwingWorker<Void, Void> {

    @Override
    protected Void doInBackground() throws Exception {
        //do something

        return null;
    }

}

public class ClassSwingW3 extends SwingWorker<Void, Void> {

    @Override
    protected Void doInBackground() throws Exception {
        //do something

        return null;
    }

}

I want the csw2 to execute after csw1 is done, and csw3 to execute after csw2 is done. I do not want them executing at the same time. How would I accomplish this? Thank you

like image 372
jadrijan Avatar asked Apr 30 '12 16:04

jadrijan


1 Answers

You could use the get() method instead of execute() - it will block until the SwingWorker finishes its job. Just make sure that you don't call it from the EDT.

Javadoc extract:

Waits if necessary for the computation to complete, and then retrieves its result. Note: calling get on the Event Dispatch Thread blocks all events, including repaints, from being processed until this SwingWorker is complete.

like image 159
assylias Avatar answered Nov 02 '22 05:11

assylias