I'm new here, sorry if this has been handled somewhere I haven't looked yet.
I'm building a Sudoku Solver in Java as a learning project and I'm allowing the user to enter an 81 character string in the top left TextField on the form as a quicker way to load the game onto the board. I know there has to be a more concise way of doing this. My current plan is:
.Initialize all 81 TextFields in fxml individually.
<TextField fx:id="tf24" maxWidth="21.5" > </TextField>
<TextField fx:id="tf25" maxWidth="21.5" > </TextField>
<TextField fx:id="tf26" maxWidth="21.5" > </TextField>
.Have a method in the Controller Class that passes the data to the Main class for later use and also breaks down the 81 character string and prints it to the game board.
public void getBoard() {
if (tf11.getLength() == 81) {
String gameString = tf11.getText();
for (int x = 81; x >= 1; x--) {
int correction = ((x + 10) + ((x-1) / 9));
Main.inputBoard(gameString, correction);
gameString = gameString.substring(0, gameString.length() - 1);
}
.
.
.
tf26.setText(tf11.getText().substring(tf11.getLength()-1));
tf11.setText(tf11.getText(0,tf11.getLength()-1));
tf25.setText(tf11.getText().substring(tf11.getLength()-1));
tf11.setText(tf11.getText(0,tf11.getLength()-1));
tf24.setText(tf11.getText().substring(tf11.getLength()-1));
tf11.setText(tf11.getText(0,tf11.getLength()-1));
.
.
.
}
}
.Have a method in the Main class that accepts the data and stores it in an array.
public static void inputBoard(String gameString, int correction) {
board[correction] = gameString.substring(gameString.length()-1);
}
I've been struggling for 2 days trying to find a better way to do this. It would be very easy if I could call the setText method of the TextField by referencing a string which contains the fx:id name of the TextField, but I have not figured any good way to do this. Things I've read to try to handle the problem myself include:
How do I invoke a Java method when given the method name as a string?
How to create an array of TextFields in JavaFX
https://docs.oracle.com/javase/7/docs/api/java/awt/TextField.html
To initialize or to not initialize JavaFX TextField
Any guidance would be greatly appreciated!