What i want to accomplish is:
*A window containing a matrix of buttons. Let's say 10x10.
*The buttons should say either "1" or "0", and change when I click them.
*The values of the buttons (1 or 0) should be stored in a String[][]
matrix.
At the moment I have a String[][]
2D-array containing values. I can show it in a window with clickable buttons using the following code:
//dim = 10
//matrix is the 10x10 String[][] matrix containing 1s or 0s
private static void convertMatrixToGUI() {
JFrame f = new JFrame("Window containing a matrix");
JPanel p = new JPanel();
p.setLayout(new GridLayout(dim, dim));
for(int r = 0; r < dim; r++){
for(int c = 0; c < dim; c++){
p.add(new JButton(matrix[r][c]));
}
}
f.add(p);
f.pack();
f.setVisible(true);
}
The next step is to change the values in the matrix when clicking the buttons. If i click a 0 it should change it to a 1 and vice versa. The values have to be stored in the String[][]
at all times.
How do i change things in the string matrix by clicking a button in the graphical matrix? If i click the button at position [5][2]
, how should the program know that I want to change the string matrix as position [5][2]
?
Best regards Goatcat