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 😅
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
@@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?
@@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 :)
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
The mighty cure for our not-updating-obejcts in obersvable lists
Thanks, bro. You helped me SOOOOOOOOOOOOOOOOO MUCHHHHHHHH
perfect and breat job thanks this can help me for my project
Could we make the MainModel into a Singleton?
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 😅
hehehe Intellij on white background, so dangerous for the eyes, its like the netbeans guys xd ...
😆
hello! can we somehow make listView editable and make it change our model?
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
@@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?
@@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 :)
@@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!
@@JavaHandsOnTeaching Thanks, you helped me too (a lot) both with the video and the link
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
You should not need to cast if you declare the table view with Person in the diamond notation.
@@JavaHandsOnTeachinglol exactly