You have 2 options:
A) Save the text fields in the JTextField[][] array.
B) Use reflection
Since A has already been explained in other answers, I will focus only on B
Reflection in Java can be used to convert the name of some field to the field itself, of a kind.
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 in the same class that has the declared fields a00, a01, a02, ... It will also get a JTextField that is stored in your field, so you cannot use this to set fields. For this, the silylar kit method can be used.
However, using reflection in Java is generally considered bad practice. Here, solution A can and should be used, but if you are lazy and do not want to rewrite your code, you can use this.
source share