title: Reset Application with Swing button
body: <p>Trying to reset the buttons with a "Play again" JButton.</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  import java.awt.Toolkit;
  import java.awt.Image;
  
  public class HW4_TicTacToePanel extends JFrame implements ActionListener {
  
  // declare variables and arrays
      private int counter = 0;
      private String letter;
      private boolean win = false;
      private int[][] winConditions = new int[][]{
          {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //Horizontal wins
          {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //Vertical wins
          {0, 4, 8}, {2, 4, 6} //Diagonal wins
      };
      // JFrame Instructions
      //  private JButton buttons[] = new JButton[12];
      //  private JFrame gamewindow = new JFrame("Tic Tac Toe");
      // End JFrame
      // Panel demo
      private JPanel southPanel; //panel for replay and exit button
      private JButton[] optButtons;//Exit and replay buttons
      private JPanel centerPanel;
      private JButton[] gameButtons;//Tic tac toe squares
      private GridLayout myGridLayout;
  
      // End panel demo
      public HW4_TicTacToePanel() //Constructor
      {
          super("Tic Tac Toe"); //super calls the JFrame controller
          southPanel = new JPanel();// instantiate panel
          optButtons = new JButton[2];//create an array of 2 buttons
          String[] buttonNames = {"Play Again", "Exit"};
  
          centerPanel = new JPanel(); //instantiate panel
          gameButtons = new JButton[9];//creates an array of 9 buttons
          myGridLayout = new GridLayout(3, 3, 2, 2);
  
          setLayout(new BorderLayout());//set layout for JPanel
          centerPanel.setLayout(myGridLayout);//set Layout for center panel
  
          // add buttons to the centerPanel and add the ActionListener
          for (int i = 0; i &lt; gameButtons.length; i++) {
              gameButtons[i] = new JButton();
              centerPanel.add(gameButtons[i]);
              gameButtons[i].addActionListener(this);
          }
  
          add(centerPanel, BorderLayout.CENTER); //add panel to JFrame
  
          // add buttons to the southPanel and add the ActionListener
          for (int i = 0; i &lt; optButtons.length; i++) {
              optButtons[i] = new JButton(buttonNames[i]);
              southPanel.add(optButtons[i]);
          }
  
          //add functionality to replay and exit buttons
          optButtons[0].addActionListener(this);
          optButtons[1].addActionListener(this);
  
          add(southPanel, BorderLayout.SOUTH); //adds panel to JFrame
      }
  
      public void actionPerformed(ActionEvent a) {
          // Add Image icons
          counter++;
  
           //Calculate whose turn it is by using modulus to determine even or odd
      if (counter % 2 == 0) {
          letter = "O";
          Icon o = new ImageIcon(getClass().getResource("O.gif"));
          //Capture and show player input then disable button so it may not be reselected
          JButton pressedButton = (JButton) a.getSource();
          pressedButton.setIcon(o);
          pressedButton.setText(letter);
          pressedButton.setEnabled(false);
      } else if (a.getSource() == optButtons[0])
      {
          //
          //play again Instructions
          //
          System.exit(0);
      } else if (a.getSource() == optButtons[1])
      {
          System.exit(0);
      } else {
          letter = "X";
          Icon x = new ImageIcon(getClass().getResource("X.gif"));
          //Capture and show player input then disable button so it may not be reselected
          JButton pressedButton = (JButton) a.getSource();
          pressedButton.setIcon(x);
          pressedButton.setText(letter);
          pressedButton.setEnabled(false);
      }
  
  //determine who won
          for (int i = 0; i &lt;= 7; i++) {
              if (gameButtons[winConditions[i][0]].getText().equals(gameButtons[winConditions[i][1]].getText())
                      &amp; gameButtons[winConditions[i][1]].getText().equals(gameButtons[winConditions[i][2]].getText())
                      &amp; gameButtons[winConditions[i][0]].getText() != "") {
                  win = true;
              }
          }
  
  //Show victor dialog
          if (win == true) {
              JOptionPane.showMessageDialog(null, letter + " wins the game!");
              //Remove once optButtons are operational...
              System.exit(0);
          } else if (counter == 9 &amp;&amp; win == false) {
              JOptionPane.showMessageDialog(null, "The game was a tie!");
              //Remove once optButtons are operational...
              System.exit(0);
          }
      }
  
      public static void main(String[] args) {
          HW4_TicTacToePanel starter = new HW4_TicTacToePanel();
      }
  }
  </code></pre>
  
  <p>For the Play Again button I was thinking of reseting the counter and nullifying any values stored in the game buttons. I would implement this in the same loop as the player moves as an else if statement.</p>
  
  <p>I've been working on this for a little while to no avail, any advice?
  Thanks</p>