title: How to implement the wait(); statement properly?
body: <p>I'm getting an issue when I'm trying to make a tic tac toe game. The Issue that I'm coming across is presented when I'm trying to use a wait(); statement in order to pause the game so the player is able to look at who won the game and what the winning squares are, however when I try to run this little chunk of code right here:</p>
  <pre><code>            wait(1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  </code></pre>
  <p>It gives me this error code right here:</p>
  <pre><code>Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.IllegalMonitorStateException
      at java.lang.Object.wait(Native Method)
      at GameCode.restart(GameCode.java:264)
      at GameCode.xWins(GameCode.java:245)
      at GameCode.check(GameCode.java:171)
      at GameCode.actionPerformed(GameCode.java:70)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$500(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>
  <p>It describes it to be an <code>IllegalMonitorStateException</code>, however I tried using <code>throw</code> to resolve this problem but it still continues to give me this error. Any solutions out there? For reference here is my code if you need to see any other info:</p>
  <pre><code>import java.util.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class GameCode implements ActionListener {
      
      Random random = new Random();
      JFrame frame = new JFrame();
      JPanel title_panel = new JPanel();
      JPanel button_panel = new JPanel();
      JLabel textfield = new JLabel();
      JButton[] buttons = new JButton[9];
      boolean player1_turn;
      
      
      
      
      GameCode() {
          
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(500,500);
          frame.getContentPane().setBackground(new Color(0,0,0));
          frame.setLayout(new BorderLayout());
          frame.setVisible(true);
          
          textfield.setBackground(new Color(25,25,25));
          textfield.setForeground(new Color(25,255,0));
          textfield.setFont(new Font(&quot;TimesRoman&quot;, Font.BOLD,75));
          textfield.setHorizontalAlignment(JLabel.CENTER);
          textfield.setText(&quot;Tic Tac Toe&quot;);
          textfield.setOpaque(true);
          title_panel.setLayout(new BorderLayout());
          title_panel.setBounds(0,0,800,100);
          
          button_panel.setLayout(new GridLayout(3,3));
          button_panel.setBackground(new Color(150,25,25));
          
          for(int j = 0; j &lt; 9; j++ ) {
              
              buttons[j] = new JButton();
              button_panel.add(buttons[j]);
              buttons[j].setFont(new Font(&quot;Ink Free&quot;, Font.BOLD,120));
              buttons[j].setFocusable(false);
              buttons[j].addActionListener(this);
              
          }
          
          
          title_panel.add(textfield);
          frame.add(title_panel, BorderLayout.NORTH);
          frame.add(button_panel);
          
          Turnone();
          
      }
  
      @Override
      public void actionPerformed(ActionEvent arg0) {
          
          for (int i = 0; i &lt; 9; i++) {
              
              if(arg0.getSource()==buttons[i]) {
                  if(player1_turn) {
                      if(buttons[i].getText()==&quot;&quot;) {
                          buttons[i].setForeground(new Color(255,0,0));
                          buttons[i].setText(&quot;X&quot;);
                          player1_turn=false;
                          textfield.setText(&quot;O turn&quot;);
                          check();
                      }
                  } else {
                      if(buttons[i].getText()==&quot;&quot;) {
                          buttons[i].setForeground(new Color(0,0,255));
                          buttons[i].setText(&quot;O&quot;);
                          player1_turn=true;
                          textfield.setText(&quot;X turn&quot;);
                          check();
                      }
                  }
              }
          }
      }//Creating the Buttons the player will interact with
      
      public void Turnone() {
          
          for(int i=0; i&lt;9;i++) {
              buttons[i].setEnabled(false);
          }
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          
          for(int i=0; i&lt;9;i++) {
              buttons[i].setEnabled(true);
          }
          
          if(random.nextInt(2)==0) {
              
              player1_turn = true;
              textfield.setText(&quot;X turn&quot;);
              
          } else {
              
              player1_turn = false;
              textfield.setText(&quot;O Turn&quot;);
              
          }
          
      }//Using a random int to decide which player will go first
      
      public void check() {
          //X Wins
          
          if(
                  (buttons[0].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[1].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[2].getText()==&quot;X&quot;) 
                  ) {
              xWins(0,1,2);
          }
          if(
                  (buttons[3].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[5].getText()==&quot;X&quot;) 
                  ) {
              xWins(3,4,5);
          }
          if(
                  (buttons[6].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[7].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[8].getText()==&quot;X&quot;) 
                  ) {
              xWins(6,7,8);
          }
          if(
                  (buttons[0].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[3].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[6].getText()==&quot;X&quot;) 
                  ) {
              xWins(0,3,6);
          }
          if(
                  (buttons[1].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[7].getText()==&quot;X&quot;) 
                  ) {
              xWins(1,4,7);
          }
          if(
                  (buttons[2].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[5].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[8].getText()==&quot;X&quot;) 
                  ) {
              xWins(2,5,8);
          }
          if(
                  (buttons[0].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[8].getText()==&quot;X&quot;) 
                  ) {
              xWins(0,4,8);
          }
          if(
                  (buttons[2].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;X&quot;) &amp;&amp;
                  (buttons[6].getText()==&quot;X&quot;) 
                  ) {
              xWins(2,4,6);
          }
          //O Wins
          
          if(
                  (buttons[0].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[1].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[2].getText()==&quot;O&quot;) 
                  ) {
              oWins(0,1,2);
          }
          if(
                  (buttons[3].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[5].getText()==&quot;O&quot;) 
                  ) {
              oWins(3,4,5);
          }
          if(
                  (buttons[6].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[7].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[8].getText()==&quot;O&quot;) 
                  ) {
              oWins(6,7,8);
          }
          if(
                  (buttons[0].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[3].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[6].getText()==&quot;O&quot;) 
                  ) {
              oWins(0,3,6);
          }
          if(
                  (buttons[1].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[7].getText()==&quot;O&quot;) 
                  ) {
              oWins(1,4,7);
          }
          if(
                  (buttons[2].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[5].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[8].getText()==&quot;O&quot;) 
                  ) {
              oWins(2,5,8);
          }
          if(
                  (buttons[0].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[8].getText()==&quot;O&quot;) 
                  ) {
              oWins(0,4,8);
          }
          if(
                  (buttons[2].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[4].getText()==&quot;O&quot;) &amp;&amp;
                  (buttons[6].getText()==&quot;O&quot;) 
                  ) {
              oWins(2,4,6);
          }
      
      }//Stating all the win conditions for the game
      
      public void xWins(int a, int b, int c) {
          buttons[a].setBackground(Color.GREEN);
          buttons[b].setBackground(Color.GREEN);
          buttons[c].setBackground(Color.GREEN);
          
          for(int i=0; i&lt;9;i++) {
              buttons[i].setEnabled(false);
          }
          
          textfield.setText(&quot;X Wins!&quot;);
          
          restart();
      }//When Player X wins
      
      public void oWins(int a, int b, int c) {
          buttons[a].setBackground(Color.GREEN);
          buttons[b].setBackground(Color.GREEN);
          buttons[c].setBackground(Color.GREEN);
          
          for(int i=0; i&lt;9;i++) {
              buttons[i].setEnabled(false);
          }
          textfield.setText(&quot;O Wins!&quot;);
          
          restart();
      }//When Player O wins 
      
      public void restart() { 
          
          try {
              wait(1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          
          
      }
      
      
      
  }//GameCode
  </code></pre>