JavaFX and MVC with Observable lists holding Observable objects

Поділитися
Вставка
  • Опубліковано 11 гру 2024

КОМЕНТАРІ • 16

  • @adamlorincz4858
    @adamlorincz4858 2 роки тому +1

    The mighty cure for our not-updating-obejcts in obersvable lists

  • @leivashenriquedelucenaaqui1126

    Thanks, bro. You helped me SOOOOOOOOOOOOOOOOO MUCHHHHHHHH

  • @receptionkamal7195
    @receptionkamal7195 2 роки тому +1

    perfect and breat job thanks this can help me for my project

  • @gerdsfargen6687
    @gerdsfargen6687 Рік тому +1

    Could we make the MainModel into a Singleton?

    • @JavaHandsOnTeaching
      @JavaHandsOnTeaching  Рік тому +1

      This is an excellent question.
      Short answer is YES. It solves the problem of sharing instances.
      If you are a beginner to object oriented programming, I think it is a nice way to start thinking in terms of patterns and also an easy way to solve a complex problem.
      However if you talk to more experienced programmers, they have learned that Singleton pattern in general is more of an anti pattern, due to problems with especially testability and concurrency (threads). However there is an excellent explanation here by "Aaronaught":
      softwareengineering.stackexchange.com/questions/40373/so-singletons-are-bad-then-what
      But as I said, as a beginner I would recommend just going for the easy fix (Singleton) and then seeing the problems as they arise when your programs get larger, more complex, and requires automated testing.
      In the specific case of the video, the method I am using is an ok solution, which has less drawbacks than singleton. Optimally I would use dependency injection, by changing the constructor of the controller to have the needed information (the model instance), however I cannot do that easily in JavaFX as I am not controlling the construction of the controller, as this is done by the FXML loader. The FXML loader will call only the constructor with no arguments. By getting a reference for the controller and then setting the model afterwards, I have full control, however the drawback is that if I forget to call setModel(mainModel) on the controller, the program will crash on rumtime with a null pointer exception. If I was able to have the model as part of the controllers constructor, then I could achieve compile time check if the model is being passed.
      Hope this makes sense 😅

  • @Victor-mx1jt
    @Victor-mx1jt 2 роки тому +3

    hehehe Intellij on white background, so dangerous for the eyes, its like the netbeans guys xd ...

  • @maxasimov4313
    @maxasimov4313 2 роки тому +1

    hello! can we somehow make listView editable and make it change our model?

    • @JavaHandsOnTeaching
      @JavaHandsOnTeaching  2 роки тому +1

      Yes. You need to set the property setEditable to true and use the cellValueFactory. See this example (not mine, but looks right) gist.github.com/tarrsalah/5492452

    • @maxasimov4313
      @maxasimov4313 2 роки тому +1

      @@JavaHandsOnTeaching I've tried this example!) I get cast error, because cellValueFactory works with strings, but I have my own model entity class in listView (like Person class in your videos). I've tried to set StringProperty name in my own class, but it didn't work... But how can cellValueFactory factory change for example name of our Person, if we use toString method?

    • @JavaHandsOnTeaching
      @JavaHandsOnTeaching  2 роки тому +1

      @@maxasimov4313 ah, you are further than I thought. Try looking here then:
      stackoverflow.com/questions/27438629/listview-with-custom-content-in-javafx
      This is a more advanced example than you need (custom cells), but maybe it will help you anyway. I don't have access to a computer right now, so cannot give a more precise answer right now :)

    • @maxasimov4313
      @maxasimov4313 2 роки тому +3

      @@JavaHandsOnTeaching Yes, that's what I need! We are currently working on a javafx university project and you have helped us a lot. Thank you!

    • @floppy-disko2378
      @floppy-disko2378 2 роки тому

      @@JavaHandsOnTeaching Thanks, you helped me too (a lot) both with the video and the link

  • @thebigmonstaandy6644
    @thebigmonstaandy6644 Рік тому

    49:31 This works on my PC , but i casted myTable.getSelectionModel().getSelectedItem() to Person :
    Person per=(Person) myTable.getSelectionModel().getSelectedItem();
    per.setName(edtUpdate.getText());
    and i also updated ObservableList after that

    • @JavaHandsOnTeaching
      @JavaHandsOnTeaching  Рік тому +1

      You should not need to cast if you declare the table view with Person in the diamond notation.

    • @gerdsfargen6687
      @gerdsfargen6687 Рік тому

      ​@@JavaHandsOnTeachinglol exactly