I'm trying to set up a JRadioButton-Matrix, so that in each column and in each row, only one Button can be selected at a time. I have the following code:
JRadioButton[][] button = new JRadioButton[names.length][names.length];
ButtonGroup[] r = new ButtonGroup[names.length];
ButtonGroup[] c = new ButtonGroup[names.length];
for (int i = 0; i < names.length; i++) {
r[i] = new ButtonGroup();
c[i] = new ButtonGroup();
}
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names.length; j++) {
button[i][j] = new JRadioButton();
r[i].add(button[i][j]);
c[j].add(button[i][j]);
}
}
But when I execute it, only the columns behave properly (i.e. the Buttons in the groups of c). When I comment the parts with c, however, the rows do behave properly.
To clear things a bit up (thanks to peeskillet):
Lets say I have this 4 x 4 matrix of JRadioButtons:
O O O O
O O O O
O O O O
O O O O
And I want to make it possible to make choices like these:
X O O O X O O O O X O O
O X O O O O X O X O O O
O O X O O X O O O O O X
O O O X O O O X O O X O
In the above, each column only has one and each row only has one. The following examples would NOT be possible:
X X O O X O O O
O O O O O X O O
O O X O O X O O
O O O X O O O X
However, the problem is, I CAN choose like in the above left matrix, but not the right. If I comment the following parts:
ButtonGroup[] c = new ButtonGroup[names.length];
c[i] = new ButtonGroup();
c[j].add(button[i][j]);
then the matrix on the above right is possible, but not the left.