Zyzyx
Zyzyx

Reputation: 524

wait for GUI created by event dispatch thread

I've got a class that creates and runs a thread, which creates a GUI. I want the initial class to remain suspended until the GUI is closed (OK button for example)

I tried thread.join(); but since the GUI is created on the event dispatch thread this does not seem to work, and the class continues as the GUI pops up.

    private void CreateAndRunThread(){
        GUIMaker GM= new GUIMaker(data);
        GM.run();

        try {
            TFM.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        MessageDialog.showDialog("GM Done");
    }

thread's GUI creation:

@Override
public void run() {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE); 
            CreateAndShowGUI(frame); //adds frame, packs and sets visible
        }
    }); 
}

Upvotes: 0

Views: 108

Answers (1)

Guts
Guts

Reputation: 768

Use CountDownLatch:

CountDownLatch latch = new CountDownLatch(1);

Call the following in the inital class to block:

latch.await();

Call the following when the GUI is closed:

latch.countDown();

Also it seems that you are not starting thread properly. You need to call GM.start() method instead of GM.run().

Upvotes: 1

Related Questions