Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:49:11+00:00 2026-06-08T11:49:11+00:00

I am trying to make a Tic Tac Toe program with java swing, and

  • 0

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);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:49:12+00:00Added an answer on June 8, 2026 at 11:49 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a book and am trying to make a game of tic-tac-toe
I am trying to make a really simple Tic-Tac-Toe game. I have stored values
I was trying to make Tic Tac Toe game for the monthly tutorial and
I am trying to make tic tac toe using websockets running on glassfish. I've
I've posted here earlier about how to make a tic tac toe for two
Trying to make a sliding panel where you can click next and back to
Trying to make a simple toggle menu, and I can't seem to hide/show the
I'm trying make one treeView with infinite subgroups. I can add my groups but
Trying to make a Grid Like this using java and I'm assuming a 2d
trying to make 5 curl childs for curl handler and defining them, but can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.