1

I'm trying to use a GridPane for a Sudoku 9x9 using FXML.

I'm fairly new in this field so I wanted to ask you guys before proceeding.

I've created a GridPane 9x9 and for now I'm using TextField in each node to display numbers and enabling the user to write a new number themselves.

My question:

Is it okay to create the 81 TextField's in pure java code or is there a way to do it efficiently using FXML so I don't split my view (Model-View-Controller) setup up?

Thanks in advance and sorry if my question is unclear, I'm having a hard time explaining it tbh.

ElloU
  • 67
  • 1
  • 10

1 Answers1

0

Fxml is not the only (and in my opinion not the best way) to use the model view controller pattern in javafx.

The View basically describes how the application / windows / sections look. That can be done with pure fxml, but very often it won't be enough. Especially when you want / need dynamically built content (which in your case you do not really need). So you COULD just add those 9 grid rows and 9 grid columns and add TextFields into them using FXML. However this seems very ineffective, since you could use loops and would remove a lot of time wasters while programming.

You can do that by either using a mix of fxml and code: First load the fxml in your view class and then add to it using code.

Or you could use pure code (my preferred way, since i do not like using fxml if I can't do it in fxml only - and since that is often the case for me i just skip it).

The Model represents your data. Changes in that data will change the view. This is done with observers and binds in the view class.

The Controller manipulates data. Most commonly this is done using user input, but could also be achieved using other events.


However: Keep in mind that MVC is only a very loose pattern and does not define a clear way to do it, since it is not tailored to a specific language / framework. While there are tutorials out there that try to teach how to implement the MVC pattern in JavaFX they all interpret MVC differently and as a result the implementation is also vastly different.

I hope my description of how to structure the View in JavaFX using three different approaches helps you. If not please comment and I'll try to elaborate. :)

JohnRW
  • 748
  • 7
  • 22
  • Wow - Thank you a lot! That was a very elaborate answer mate. I'm not quite ready to scrap everything I've done with FXML yet, but it's nice to know that it's alright to mix pure java code and fxml if it's necessary. Later down the road I might decide to try and do everything in pure code, but the easyness of using fxml is just very helpful to me right now. Again, thanks a lot for the very good answer! – ElloU Mar 03 '16 at 21:19
  • Glad I could be of help. God knows I struggled with this myself, because the tutorials out there suck at explaining this. Had to read a lot about mvc and it's different approaches before knowing exactly how to use it in the javafx context. If this answer answers your question, please consider upvoting it and selecting it as the answer ;) – JohnRW Mar 04 '16 at 07:39
  • And there's sooo much reading material that you just get lost in it sometimes. :) Thanks for reminding me about selecting the answer - I'm new as a user on stackoverflow ^^ – ElloU Mar 04 '16 at 11:24
  • If you wouldn't mind helping me once again JohnRW. I've created my buttons using Java Code and everything seems correct. During the creation of the buttons I add them to a GridPane and use the setOnAction method to "link" every button to a method I created to handle when a user clicks on a Button. I struggled a bit with this but then I came across this code: button.setOnAction(this::handleActionOnButtonMethod); – ElloU Mar 09 '16 at 18:09
  • Cont. the handleActionOnButtonMethod is a method I created to run some code when the button is pressed. It takes an ActionEvent as a parameter. Apparently this works, but I've never seen this syntax before - this::method - Could you explain to me how this works? :) – ElloU Mar 09 '16 at 18:09
  • This answer, answers it better then anything I could come up with. But in short: it's a special lambda syntax or more correct a method reference: http://stackoverflow.com/a/20001866/1299690 – JohnRW Mar 10 '16 at 18:20
  • Ey, JohnRW - Not sure if you get a notifcation when I write here, but here goes: I've run into a new problem. I got all my buttons working which is nice, but now I would really want a line to seperate the buttons on every 3 row and column. Just like a normal sudoku game, so you kinda seperate every 3x3 "box". Do you know what I mean? Anyways, I've tried to approach this problem by looking into css styling the buttons. My thinking was that I could style the correct buttons to a think border on the appropiate side or maybe insert a line, but I can't seem to find out how to do it. – ElloU Apr 26 '16 at 19:54