0

I am trying to create a small tic tac toe game using a 2D array of JToggleButtons, I got the general layout and have a 3x3 grid of JToggleButtons and added an ActionListener to each button when they are added to the window. However whatever I try I cant seem to get an Icon change on any of the JToggleButtons at all. How exactly can I change the icon and check that it was selected? I have 1 class Board.java, a main class that init's the Board, and a class ButtonListener that implements ActionListener.

Board.java:

public class Board extends JPanel{

private JToggleButton[][] grid = new JToggleButton[3][3];
private final int BOARD_WIDTH = 800;
private final int BOARD_HEIGHT = 600;

static Icon oIcon = new ImageIcon("src/TicTacToeBoard/assets/16894.png");
static Icon xIcon = new ImageIcon("src/TicTacToeBoard/assets/1828778.png");
JFrame board;

JPanel panel;

public Board(){
    initBoard();
}

private void makePanel(){
    panel.setBackground(Color.lightGray);
    panel.setLayout(new GridLayout(3,3));
    setBoard();
    board.add(panel);
}

private void initBoard(){

    board = new JFrame();
    panel = new JPanel();

    board.setTitle("JTicTacToe");
    board.setSize(BOARD_WIDTH, BOARD_HEIGHT);
    board.setResizable(false);
    board.setLocationRelativeTo(null);
    board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    makePanel();

    board.setVisible(true);

}

private void setBoard(){

    //Loop through 2D array
    for(int row = 0; row < grid.length; row++){
        for(int col = 0; col < grid[row].length; col++){
            grid[row][col] = new JToggleButton();
            grid[row][col].setBackground(Color.BLACK);
            grid[row][col].setForeground(Color.white);
            grid[row][col].putClientProperty("row", row);
            grid[row][col].putClientProperty("column",col);
            grid[row][col].addActionListener(new ButtonListener());
            panel.add(grid[row][col]);
        }
    }
}

protected class ButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e){
        JToggleButton button = (JToggleButton) e.getSource();

        //Just to see if I can set the icon after click
        if(button.isSelected()){
            button.setIcon(Board.oIcon);
        }

        //Test to see if I can get some sort of output in console after each toggle button is clicked
        //But for some reason I can't change the icons? why?
        System.out.println("clicked column " + button.getClientProperty("column")
                + ", row " + button.getClientProperty("row"));
    }

}

}

Cr3 D
  • 153
  • 6
  • 2
    [`JToggleButton#setIcon`](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/javax/swing/AbstractButton.html#setIcon(javax.swing.Icon))? – MadProgrammer Jul 18 '22 at 00:03
  • 3
    I believe you would need to use the `setSelectedIcon(...)` method. Or maybe you would use a JButton and disable it when clicked (so it can't be clicked again) and then set the disable icon? – camickr Jul 18 '22 at 00:05
  • Also consider an `ItemListener`, examined [here](https://stackoverflow.com/q/7877576/230513). – trashgod Jul 18 '22 at 17:33
  • When adding an ItemListener and using the setSelectedIcon method, none of these seem to work and I am not sure why, I used these before after looking through various pages but nothing is working, could it be that I am using a 2D array? I can't even change the colors of the buttons. – Cr3 D Jul 18 '22 at 23:50
  • I should add this is on MAC OS but I am not sure if that changes anything – Cr3 D Jul 19 '22 at 00:18

0 Answers0