I have this 2D JButton array (color white), and what I want to do is when I click on a square, it’ll turn into orange. When I click another square, that square becomes orange as well but the first square I clicked will return to its normal white color. How do I do it without having all the squares turn into orange?
Here’s a visualization:

Btw, when I click the squares with the numbers, their colors do not change, think of them as like setEnabled(false) buttons. I already know how to change the colors of the buttons, I just want to know how the color gets passed on square after square.
Thanks to anyone who can help!
Edit: My “attempt” but it’s making all of my null valued buttons orange.
button[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
for(int i = 0; i < button.length; i++)
for(int j = 0; j < button.length; j++){
if(g.gameBoard[i][j] == null){
//must find the position where the user clicks and make it orange
button[i][j].setBackground(Color.ORANGE);
}
}
}
});
you can use JToggleButton and put them in a ButtonGroup so that you can treat them as a group and set the color to the activated one.
P.S. i will keep in contact with this thread for the next 24 hours if there is any question i will try to explain more.