0

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!

1 Answers1

0

Ok, but why you wan't create your own class with fields that you need, for example with id and etc You can extend TextField if you need And after that you get TextField and your unique id

I don't think that you can get fx:id properly

As I can see you mixed up a lot of logic - you need separate it

Try to create own class and extends from TextField and work with new extended TextField

I think it should fix your problem

aarexer
  • 454
  • 3
  • 16
  • I appreciate the suggestion to consolidate classes. I've done this, but I am still stuck trying to optimize this: tf49.setText(board[49]); tf48.setText(board[48]); tf47.setText(board[47]); tf46.setText(board[46]); tf45.setText(board[45]); tf44.setText(board[44]); tf43.setText(board[43]); – QuiltDaddy Dec 19 '18 at 23:06
  • Why you can't create array or list of TextFields? And do your logic in loop. Maybe you should create your TextFields in code, by plain java? Refuse to create fields in fxml(maybe only in this part). – aarexer Dec 24 '18 at 19:06
  • You can mix fxml and java, some static part create in fxml, some dynamic in java – aarexer Dec 24 '18 at 19:07
  • Thanks for responding aarexer. Can you show me what you're talking about? I've tried to initialize the variables in loop and bind them to the actual textFields as well as using a generic setText method for all the textFields with no success. Any help would be appreciated. – QuiltDaddy Dec 26 '18 at 18:58
  • Smth like this for init: TextField[] textFields = new TextField[50]; for (int i = 0; i < textFields.length; i++) { textFields[i] = new TextField("TF" + i); } And smth like it for board inserts: for (int i = 0; i < textFields.length; i++) { textFields[i].setText(board[i]); } – aarexer Dec 26 '18 at 19:54
  • I appreciate the help. I've tried as specified and it compiles, but it does not associate the textField array with the actual textFields on the form. So when I use the set method nothing happens with no errors. – QuiltDaddy Dec 27 '18 at 01:12
  • And how textFields on the form correlates with textFields in array? I don't understand logic – aarexer Dec 27 '18 at 14:42
  • The logic is I'm trying to print an array of data to textFields on my form. I've tried various ways including your way. I've found no other way to do this except (textField name).setText("text"). Using an array, the program doesn't know to associate the array names with the textFields on the form, so assigning values to the array does only that and doesn't know to print those values to a textField. I can make it work by writing everything out literally, I'm just trying to find a better way than to have 81 lines of code to read the textFields and 81 lines of code to print to the textFields. – QuiltDaddy Dec 27 '18 at 16:27
  • You have array of TextFields and array of names and how i see in example above - name 81 is the name for text field 81, why we can't set names in loop? – aarexer Dec 28 '18 at 11:21
  • I am trying to figure that out too. You can try it, it doesn't work. I don't know why it doesn't work. – QuiltDaddy Dec 28 '18 at 17:52
  • See my comment here https://github.com/QuiltDaddy/Sudoku/commit/c60b7703f099f6ccdc7acdac785ad793ef464866 – aarexer Jan 01 '19 at 14:59
  • I appreciate the help. I've tried using arrays for this. This doesn't work. – QuiltDaddy Jan 02 '19 at 19:44
  • I post example for you – aarexer Jan 04 '19 at 17:44
  • I finally got it! Thank you so much for sticking with me and helping me through this. I had a few hours to play around with it today and it was obviously a misunderstanding on my part. I think it was that I was unfamiliar with the GridPane root and GridPane vbox objects and how they act as containers for the form. Also adding the textFields to the container was a necessary step to getting too. So thanks again for the works of help, much appreciated!!! – QuiltDaddy Jan 10 '19 at 03:35