0

During my researchs, I found this question here : Applying MVC With JavaFx where James_D is talking about a clear approach about MVP in JavaFX (Full APP here : https://github.com/james-d/SimpleMVP).

I read and understand the code and it really helped me but it is only here a way to "show and propose to edit" data to the user, using binding etc... But what if I want to implement an add feature to the James_D's MVP example ?

Let us supposed that I create a new view, owning DataModel in attribute, with fields reading user input etc... Then I create a new Controller handling this interface and performing task on model. I would need to add a method addPerson(Person p) in the DataModel just to add a Person to the ObservableList, is that a good idea updating the UI through the model ? (I have been told here JavaFX - MVC Application best practices with database that updating UI through model was not a good practice).

Thank you in advance for you help,

Sincerely, Paul

  • I don't see anywhere in the linked question where it says "updating the UI through the model is not a good practice". The whole point of MVC/MVP/MVVM etc type architectures is that the model is observable, and the views (or controller/presenter, depending on the MVC variant you are using) observe the model and update the UI accordingly. I recommend reading https://martinfowler.com/eaaDev/uiArchs.html (This question really seems a bit broad and perhaps too opinion-based for this forum, BTW.) – James_D Jan 16 '18 at 16:40
  • Anyway: *"I would need to add a method `addPerson(Person p)` in the `DataModel` just to add a `Person` to the `ObservableList`"*. This is not true: with the model as it is currently defined, you could just do `getPersonList().add(...)`, and the table that is currently there would update automatically. – James_D Jan 16 '18 at 16:43
  • Oh okay I see, but if I need to perform other task in the DataModel with this new Person p (save it in a database for example), does the `addPerson(Person p)` a correct way to do it ? – Paul Thirozier Jan 16 '18 at 17:37
  • 2
    It depends how you want your UI model to interact with the backend database. If you want all changes to be propagated back to the database immediately, then you would probably expose an *unmodifiable* `ObservableList` in the model, along with explicit methods to add and remove elements. Those methods would try to update the database (probably on a background thread) and then only update the observable list upon success. OTOH if you wanted to only update the database on an explicit "save" action, you would use the model as I have it and add "save" functionality to update the DB. – James_D Jan 16 '18 at 17:41
  • Ok thank you for your advice have a nice day :) – Paul Thirozier Jan 16 '18 at 17:45

0 Answers0