user1494890
user1494890

Reputation:

JButton array to int array

I am trying to make a Tic Tac Toe program with java swing, and I have the frame made. How can I go about making the buttons in the JButton array activate the int array? I want the int array to hold the values for the spots in the Tic Tac Toe grid, so when a button is pressed, the corresponding spot in the int array will be a 0 or a 1, and the text of the button will change to an X or an O.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class TicTacToeGui extends javax.swing.JFrame implements ActionListener
{

    int[][] grid = new int[3][3];
    public final static int r = 3;
    public final static int c = 3;
    public final static int X = 0;
    public final static int O = 1;

    TicTacToeGui()
    {
        this.setTitle("Tic Tac Toe");
        JButton[][] button = new JButton[3][3];
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(r, c));
        for(int i = 0; i < r; i++)
        {
            for(int j = 0; j < c; j++)
            {
                button[i][j] = new JButton("");
                button[i][j].addActionListener(this);
                panel.add(button[i][j]);
            }

        }
        this.add(panel);
        this.setSize(400, 400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource() instanceof JButton){

        }
    }
    public static void main(String [] args)
    {
        new TicTacToeGui().setVisible(true);
    }

}

Upvotes: 0

Views: 2048

Answers (3)

aaazalea
aaazalea

Reputation: 7930

Use JButton's setActionCommand method to set the action command in button[0][0] to "00" and the command from button[2][1] to "21". this ill let you get the position easily right from actionPerformed. Also, you need three states, not just 2. If you're not sure what I'm talking about, play a game of tic-tac-toe and write down the array about halfway through.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347332

You could create your own JButton implementation and supply an index value to. That we you could extract it from the ActionListener

public void actionPerformed(ActionEvent e){
    if(e.getSource() instanceof MySuperButton){

        MySuperButton btn = (MySuperButton)e.getSource();
        int[] index = btn.getIndex();
        // or
        int row = btn.getRow();
        int col = btn.getColumn();

    }
}

Then when you set it up, you could:

for(int i = 0; i < r; i++)
{
    for(int j = 0; j < c; j++)
    {
        button[i][j] = new MySuperButton(i, j); // Store the row/column
        button[i][j].addActionListener(this);
        panel.add(button[i][j]);
    }

}

This would also allow you to store the state of the button internally...

You might also like to look at JToggleButton

Upvotes: 2

LeleDumbo
LeleDumbo

Reputation: 9340

assuming the JButton indexes mirror the int array, you could search for the JButton being pressed (e.getSource() in actionPerformed) in the button array, but you have to put button array as instance variable of the class so you can use it from other methods, esp. the actionPerformed(). Once you find the indexes, simply update it's corresponding value in the int array.

Upvotes: 0

Related Questions