Answer the question
In order to leave comments, you need to log in
How to properly code in JavaFX+FXML with MVC model?
There are three files. Standardly created in IDEA
main.java
controller.java
sample.fxml
I've already played enough with buttons and other things.
The question is how and where, according to the MVC concept, to prescribe code.
For example, when I start the program, I need the code to query the database or read the file. The received data was loaded into the ComboBox in an array. Subsequently, the application interface reacts to the selected option from the drop-down list - this is already clear. It is in controller.java to prescribe.
I can write all the code in controller.java. But this doesn't seem to be quite correct.
In general - where to write that part of the code with a database and file poll? All sorts of constructors, arrays, variables?
Answer the question
In order to leave comments, you need to log in
domanskiy , try and master, just before that, read a book, an article about MVC, what it is, why, how and where it is used, how it is used in java and javafx in particular. I will even say differently, MVC is a generalized principle, but there are a lot of its implementations in the approach.
The question itself contradicts everything.
If you exaggerate, then one of the MVC options in javafx will look something like this:
For example, a program that should read / save a file and display the contents in a table on the form.
M (Model) - let's say it will be DataModel.class. It has 3 functions loadData(), saveData() and getData(). Implements the full logic of how to read/write a file. How to digest data, add\multiply\...
V (View) - this is directly how something will be displayed on the form, i.e. this is the FXML file itself or a class that describes the interface, where is which button, how does it look. Provides a ListView and a couple of buttons to refer to itself. Absolutely knows nothing from where and how the data is taken for display (i.e. absolutely).
C (Controller) - for example DataController.class. This class knows just provided a View (again, it just knows that there is a UI that it will draw) and a couple of buttons, again it does not know what they look like, where they are. She also knows where to get the data (from the DataModel) but absolutely does not know how she gets them. As a result, there is a junction of Representation and Data. It just takes the data from the DataModel with the getData() method and passes it to the View in the listView. Takes the load button event and calls the loadData() method on the DataModel. At the same time, once again, the controller has no idea where the button is on the form, how the data is obtained, it doesn’t give a damn about it.
As a result, each part is independent of the other. It is necessary that instead of receiving data from a file, they were taken from the database or by telepathy? Not a question, just remake DataModel.class , all other parts of the program will not even understand that something has changed. Should I send data as a web page instead of JavaFX? Please change the View and again, nothing else needs to be redone.
And now we return to the beginning, run to read literature.
And also for the record, any lengthy work must be done in the background and fill out the form with them. If the data affects the display, then there are preloaders for this (so you launch your IDE and first you see a square with a picture and a progress bar, and only then the program window is displayed).
It seems to me that you need to do this:
- all the logic of working with the database, if not displayed in a separate class, then you need to register it in main.java
- a list/array that is filled upon request from the database is the same in main
- And now filling with values from the list of the interface element - ComboBox, register in the controller in the initialization section.
Right?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question