Possible Duplicate:
How to get X and Y index of element inside GridLayout?
I have an 2D array of buttons that I wish to use. When I want to call an actionListener, how to I tell which button index in this 2D array of mine is being clicked? This is my first time dealing with a listener, so please explain this on a more basic level if you can.
Here is some code of how I have my buttons laid out on a gride (12x12)
//A loop to add a new button to each section of the board grid.
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
gameButtons[i][j] = new JButton();
gameButtons[i][j].setBackground(colors[(int)(Math.random() * numColors)]);
boardGrid.add(gameButtons[i][j]);
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) {
}
}
}
These buttons are randomly assigned a color from an array of colors created earlier. I now have to override actionlistener and I don't know how to do that in a way which allows me to get the button being pressed and compare it to other buttons around it. I would like to mention that I am dealing with static methods.