title: How can I make code wait for a GUI to finish?
body: <p>I know there is a similar question asked in relation to Matlab, but it hasn't helped me for Java. I am writing a GUI for a program where the results of one GUI frame are necessary for another GUI frame - I have simplified my problem to that below:</p>
  
  <p>I have a GUI <code> JFrame </code> with a <code> JTextField </code> that, when a button is pressed, returns the contents of the field as a string.</p>
  
  <p>Further down the static main method, this string is used for other methods - although in this demonstration its just put into a print statement.</p>
  
  <p>The problem I am having is that the code is not waiting for completion of the GUI (aka someone to press the button and trigger an event) before continuing on to the next statement - thus in this demo problem its causing "null" to be printed and in my larger problem it causes null to be passed to other JFrames (and throw an exception).</p>
  
  <p>What I have learnt on line is that this is due to the "multi-threaded" nature of Swing - and every time I create a Swing component, it runs in its own thread to enable the rest of the code to continue - however in this case, I don't want the rest of the code to continue, what I think I want (I may be wrong) is all the other threads to wait for the current Swing thread to finish and give a result which can be used further down the program.</p>
  
  <p>What code can I use in order to make the code pause until the GUI's <code>JButton</code> has been clicked?</p>
  
  <p>I am aware that one solution to this is to "nest" the dependent code inside the <code> ActionListener </code> part of the code, but in larger projects this gives me large amounts of nested code, which doesnt seem a good thing.</p>
  
  <p>The demo problem code for the main method is here: </p>
  
  <pre><code>public class Testing {
  
  public static void main(String[] args) {
  
      Testing runningClass = new Testing();
      String message = null;
  
      JGetString getString = new JGetString();
      message = runningClass.initiateListener(getString);
  
      System.out.println(message); // How can I make this wait until getString
                                      // has closed?
  
  }
  
  private String initiateListener(JGetString window) {
      buttonListener listener = new buttonListener(window);
      window.addActionListener(listener);
      return listener.returning;
  
  }
  
  public class buttonListener implements ActionListener {
  
      JGetString getString;
      String returning;
  
      public buttonListener(JGetString getString) {
          this.getString = getString;
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
          String returning = getString.returnString();
          this.returning = returning;
          getString.setVisible(false);
  
      }
  
  }
  
  }
  </code></pre>
  
  <p>And the code for my GUI which extends <code> JFrame </code> is here :</p>
  
  <pre><code>public class JGetString extends JFrame {
  private JTextField textField;
  private JButton btnGet = new JButton("Get");
  
  public JGetString() {
      textField = new JTextField();
      getContentPane().add(textField, BorderLayout.CENTER);
      getContentPane().add(btnGet, BorderLayout.SOUTH);
      setVisible(true);
      pack();
  }
  
  
  public void addActionListener(ActionListener act) {
      btnGet.addActionListener(act);
  
  }
  
  public String returnString() {
      return textField.getText();
  }
  
  
  }
  </code></pre>
  
  <p>Sorry for such a long question and large blocks of code - and thanks!</p>