I have 36 JButton
components in a grid on a JFrame
, and wish to set their text to 1, 2, 3 ... 36 when I open the frame from a menu which is on another frame. (Later I have to randomize their number.)
The buttons have similar names:
jButton1
jButton2
jButton3
...
jButton35
jButton36
To simply change the first button text to 1 is:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
grid gr = new grid();
grid.jButton1.setText("1");
gr.setVisible(true);
}
Is there a way something like this?:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
grid gr = new grid();
String number;
for (int i=1; i<37; i++) {
number=Integer.toString(i);
grid.jButton<i>.setText(number);
}
gr.setVisible(true);
}
I have found these links but they weren't very useful since my buttons are not in any array or list and they are changing texts from the same frame, or is there no other way?:
Assigning variables with dynamic names in Java
How to give each JButton in a 10 x 10 grid button layout a unique ID/name