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