How to initialize JavaFX controllers with the same model object?

后端 未结 3 766
予麋鹿
予麋鹿 2021-02-01 03:43

Scenario

I am creating a GUI where multiple views reference the same model object.

What I am Accustom to

In Swing, if

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 04:34

    I have been researching this problem and I have found that dependency injection along with a singleton (for the model) is my optimal design. I learned that having a setter method for the model in each of my controllers is a form of dependency injection as is passing the model through the constructor. Spring and Guice are frameworks to help that.

    I tried using Spring as noted here: http://www.zenjava.com/2011/10/23/better-controller-injection/ but I ran into the problem when the configuration class tried to load a controller, it failed to load the member controllers. This may not have been a Spring issue, but I figured I didn't care enough to spend the time to get it working. Plus, I would have had to go through all of my controller files and edited the way they were created.

    Having searched around and doing a lot of reading, I found that this article best expresses what I would like to be able to do: https://forums.oracle.com/thread/2301217?tstart=0. Since the article is referring to suggested improvements, these improvements do not yet exist in JavaFX. But when they do, I will be implementing them. Just for an example, being able to inject a model via the fxml:

    
    

提交回复
热议问题