how to get the row/col for these buttons
Code:
// Tic Tac Toe game panel.
JPanel boardGamePanel = new JPanel();
boardGamePanel.setLayout(new GridLayout(3, 3));
JButton button[][] = new JButton[3][3];
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
button[row][col] = new JButton();
try {
button[row][col].setIcon(new ImageIcon(new URL("http://localhost:8080/eg/Images/blank.gif")));
}
catch (Exception e) { }
button[row][col].setBackground(Color.white);
button[row][col].addActionListener(this);
boardGamePanel.add(button[row][col]);
}
}
contentPane.add(boardGamePanel);
As you see, button[row][col].addActionListener(this);
but when i click on the button, how would i actually get the row and col to keep track of the coordinates that have been clicked on the grid?
Code:
public void actionPerformed(ActionEvent e) {
// when the button is clicked i want to get the row/col for that button too
}
Re: how to get the row/col for these buttons
a nested for loop on the button array then compare array items to e.getSource() if they are the same object then you've got your button