I'm trying to write my first Swing app, it is a simple chess engine in Java. I've made a grid of JButtons to represent squares. Looks ok, but I've come across a problem when trying to add ActionListeners to each square. I want to get squares co-ordinates and print it to console when its clicked. This is what I tried(I guess I don't really understand how ActionListeners work):
// chessBoardSquares[][] is an 8x8 array of Jbuttons
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
chessBoardSquares[i][j].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("x: "+i+"y: "+j);
}
});
}
}