So, I know there is this:
int number = Integer.parseInt("5");
String numtxt = Integer.toString(12);
double number = Double.parseDouble("4.5");
String numbertxt = Double.toString(8.2);
String letter = Character.toString('B');
char letter = "stringText".charAt(0);
so on...
but that I do not know how to call the String value to call, there was a JButton variable name dynamically; is it possible?
Let's say I have 4 JButton called btn1, btn2, btn3 and btnFillNumber;
I create a line called buttonName;
package testing;
public class Testing extends javax.swing.JFrame {
String buttonName;
int num;
public Testing() {
initComponents();
}
@SuppressWarnings("unchecked")
private void btnFillNumberActionPerformed(java.awt.event.ActionEvent evt) {
for(num = 1; num <= 3; num++){
buttonName = "btn" + Integer.toString(num);
JButton.parseJButton(buttonName).setText(num);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Testing().setVisible(true);
}
});
}
private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JButton btnFillNumber;
}
I know that there is no JButton.parseJButton (), I just do not want to make a complicated explanation, I just need a conversion from String to dynamically call the JButton name.
See this:
for(num = 1; num <= 3; num++){
buttonName = "btn" + Integer.toString(num);
JButton.parseJButton(buttonName).setText(num);
}
I want to make a loop using String with
- fixed string value (btn) and
- the number of increments after it (1, 2, 3 ...) and
- use to call JButton.
I just can do it, but what if I have 25 or more? So the loop I wanted ...
btn1.setText("1");
btn2.setText("2");
btn3.setText("3");
, JButtons - .
:

:

P.S. JFrame NetBeans ( , JPanel, JButton .., , , , , . , ).