Java tutorial
import java.awt.GridLayout; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Main { private static final int N = 5; private final List<JButton> list = new ArrayList<JButton>(); private JButton getGridButton(int r, int c) { int index = r * N + c; return list.get(index); } private JButton createGridButton(final int row, final int col) { final JButton b = new JButton("r" + row + ",c" + col); b.addActionListener(e -> { JButton gb = Main.this.getGridButton(row, col); System.out.println("r" + row + ",c" + col + " " + (b == gb) + " " + (b.equals(gb))); }); return b; } private JPanel createGridPanel() { JPanel p = new JPanel(new GridLayout(N, N)); for (int i = 0; i < N * N; i++) { int row = i / N; int col = i % N; JButton gb = createGridButton(row, col); list.add(gb); p.add(gb); } return p; } private void display() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(createGridPanel()); f.pack(); f.setVisible(true); } public static void main(String[] args) { new Main().display(); } }