You have 2 options:
A) Store your text fields in a JTextField[][]
array.
B) Use reflection
Since A
is already explained in other answers, I'll only focus on B
.
Reflection in Java can be used to transform name of some field to the field itself, sort of.
JTextField getField(int row, int col) {
int index = 9 * col + row; // for row-major tables
// int index = 9*row + col; // for col-major tables
String name = String.format("a%02d", index);
System.out.println("Name: " + name); //For debugging purposes
try {
Field field = Test.class.getDeclaredField(name);
return (JTextField) field.get(this);
} catch (NoSuchFieldException | SecurityException
| IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
You will need to put this code to the same class that have a00, a01, a02, ...
fields declared. Also this will only get the JTextField
that is stored in your field, so you can't use this to set the fields. Sililar set method could be used for that.
However, using reflection in Java is generally considered a bad practice. Here, solution A
can and should be used, but if you are lazy and you don't want to rewrite your code, you can use this.