As part of an exercise, I would like to design a program that displays a 9x9 grid of JButtons. When I click on a button, I would like that button to change in some way, like say originally it displays an 'o', but when you click on it, it displays an 'x', or maybe it changes colors, etc., while the other buttons remain unchanged.
However, I am not sure how to do this. I have created a 2D array of JButtons, and placed each on in a 9x9 GridLayout panel. I also set an ActionListener for each one. The problem is, I do not know how to change the color or text of only one button. Here is the short version of my program only displaying the relevant parts.
private JButton[][] t = new JButton[9][9]; //Declared much earlier in the program, right after the class declaration.
public void buildTile()
{
panelc.setLayout(new GridLayout(9, 9));
for(int r = 0; r < 9; r++)
{
for(int c = 0; c < 9; c++)
{
t[r][c] = new JButton("O");
t[r][c].setBackground(Color.BLACK);
t[r][c].setForeground(Color.WHITE);
t[r][c].addActionListener(new TileListener());
panelc.add(t[r][c]);
}
}
}
private class TileListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Some code to change a specific button
}
}
How may I specify which button to perform some sort of aesthetic change to?