title: Java wait for button press before continue
body: <p>Im trying to make a GUI Class for settings in a game, it is it's own class apart from main. in main i instantiate an object of the settings class, and i want it to wait until it takes a click before continuing the code in main. as it is, it instantiates the object and finishes the code in main without waiting for input. ive tried actionListener but cant get it to work, my guess is because i have the GUI separate from main.</p>
  
  <p>settings class</p>
  
  <pre><code>public class Settings extends JFrame implements ActionListener
  {
  private int option;
  
  public Settings()
  {
      option = 0;
      //JFrame
      setTitle("Battleship Settings");
      setVisible(true);
      int xInset = getInsets().left + getInsets().right; //right left window borders
      int yInset = getInsets().top + getInsets().bottom; //top bottom window borders
      setSize(300 + xInset, 300 + yInset);
      setLocationRelativeTo(null); //center window
  
      //Main Panel
      JPanel panel = new JPanel();
      panel.setLayout(null);
      panel.setSize(300, 300);
      panel.setBackground(Color.blue);
      add(panel);
  
      // Creation of a Panel to contain the Buttons.
      JPanel buttonPanel = new JPanel();
      buttonPanel.setLayout(null);
      buttonPanel.setLocation(0, 0);
      buttonPanel.setSize(300, 300);
      buttonPanel.setBackground(Color.red);
      panel.add(buttonPanel);
  
      //Buttons
      JButton b1 = new JButton("VS AI");
      b1.setSize(180, 30);
      int b1CenterX = (int) ((buttonPanel.getSize().getWidth() / 2) - (b1.getSize().getWidth() /2));
      b1.setLocation(b1CenterX, 145);
      b1.addActionListener(this);
      buttonPanel.add(b1);
  
      JButton b2 = new JButton("2 Players");
      b2.setSize(180, 30);
      int b2CenterX = (int) ((buttonPanel.getSize().getWidth() / 2) - (b2.getSize().getWidth() /2));
      b2.setLocation(b2CenterX, 195);
      b2.addActionListener(this);
      buttonPanel.add(b2);
  
      panel.setOpaque(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE); //if you dont exit on close, the program keeps running on the background
  }
  
  public int getOption()
  {
      return option;
  }
  
  public void setOption(int optn)
  {
      option = optn;
  }
  
  @Override
  public void actionPerformed(ActionEvent e)
  {
      if (e.getActionCommand().matches("VS AI"))
      {
          JOptionPane.showMessageDialog(null, "VS AI");
          option = 1;
          this.dispose();
      }
      else if (e.getActionCommand().matches("2 Players"))
      {
          JOptionPane.showMessageDialog(null, "2 Players");
          option = 2;
          this.dispose();
      }
      else
          JOptionPane.showMessageDialog(null, "Something went wrong");
  }
  
  }
  </code></pre>
  
  <p>and this is main</p>
  
  <pre><code>public class Main {
  
  public static void main(String[] args)
  {
      Settings BattleSettings = new Settings();
  
  
  
      JOptionPane.showMessageDialog(null, "option # " +BattleSettings.getOption());
  }
  
  }
  </code></pre>
  
  <p>the program is firing the MessageDialog in main before it has given the player a chance to select a button. i want to wait after the GUI window is created and resume the code once the player has clicked a button.</p>