Wait for Swing Interface to close before proceeding

995 Views Asked by At

I've been searching near and far for a solution to my question but I am having difficulty even defining my search terms.

I have a method that creates a Swing GUI using invokeLater where the user completes some task. Once the task is completed, the window closes and the initial calling thread (e.g. the method) should resume execution. To be more specific, here is a summary of the method:

public class dfTestCase extends JFrame{
    public dfTestCase{
          ... //GUI code here
    }

    public String run()
    {
        CountDownLatch c = new CountDownLatch(1);
        Runnable r = new Runnable()
        {
            public void run()
            {
                setVisible(true);  //make GUI visible
            }
        };

        javax.swing.SwingUtilities.invokeLater(r);

        //now wait for the GUI to finish
        try
        {
                testFinished.await();
        } catch (InterruptedException e)
        {
               e.printStackTrace();
        }

        return "method finished";
    }

    public static void main(String args[]){
          dfTestCase test = new dfTestCase();
          System.out.println(test.run());
    }
 }

Within the GUI, I have actionListeners for buttons that will close and countDown the CountDownLatch.

While the CountDownLatch works, it is not suitable for my purposes because I need to run this GUI several times and there is no way to increment the latch. I'm looking for a more elegant solution - it is my best guess that I would need to make use of threads but am unsure how to go about this.

Any help would be much appreciated!

Update Some clarification: What is happening is that an external class is calling the dfTestCase.run() function and expects a String to be returned. Essentially, the flow is linear with the external class calling dfTestCase.run()-->the GUI being invoked-->the user makes a decision and clicks a button-->control to the initial calling thread is returned and run() is completed.

For now my dirty solution is to just put a while loop with a flag to continuously poll the status of the GUI. I hope someone else can suggest a more elegant solution eventually.

public class dfTestCase extends JFrame{
    public dfTestCase{
          ... //GUI code here
          JButton button = new JButton();
          button.addActionListener{
          public void actionPerformed(ActionEvent e){
              flag = true;
          }
          }
    }

    public String run()
    {
           Runnable r = new Runnable()
           {
                public void run(){
                      setVisible(true);  //make GUI visible
           };

           javax.swing.SwingUtilities.invokeLater(r);

           //now wait for the GUI to finish
          while (!flag){
              sleep(1000);
          }
          return "method finished";
     }

     public static void main(String args[]){
           dfTestCase test = new dfTestCase();
           System.out.println(test.run());
     }
}
2

There are 2 best solutions below

0
On

For an example of using model dialogs you can check out the ParamDialog class I wrote. In particular, check out ParamDialog.getProperties(Properties);

http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/ui/dialogs/

0
On

Modal dialogs and SwingUtilities#invokeAndWait iso invokeLater should allow you to capture user input and only continue the calling thread when the UI is disposed