Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to initialize JavaFX controllers with the same model object?
    primarykey
    data
    text
    <p><strong>Scenario</strong></p> <p>I am creating a GUI where multiple views reference the same model object. </p> <p><strong>What I am Accustom to</strong> </p> <p>In Swing, if i want all the views to reference the same model i would pass the model into the constructor. </p> <p><strong>What I am Currently Doing</strong></p> <p>In JavaFX, I am passing the model around by having a setter method in the views/controllers (menubars, split panes, tabs, ...), after each view/controller has been loaded. I find this very tacky and cumbersome. Additionally, I find it won't work because in certain situations i need the model to already exist in a controller before some of the controller widgets are initialized.</p> <p><strong>Lackluster Alternatives</strong></p> <p>(Note: I am referencing these stackoverflow questions: </p> <ul> <li><a href="https://stackoverflow.com/questions/10134856/javafx-2-0-how-to-application-getparameters-in-a-controller-java-file/10136403#10136403">Javafx 2.0 How-to Application.getParameters() in a Controller.java file</a></li> <li><a href="https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310">Passing Parameters JavaFX FXML</a></li> <li><a href="https://stackoverflow.com/questions/12166786/multiple-fxml-with-controllers-share-object/">Multiple FXML with Controllers, share object</a></li> <li><p><a href="https://stackoverflow.com/questions/14370183/passing-parameters-to-a-controller-when-loading-an-fxml/14432578#14432578">Passing parameters to a controller when loading an FXML</a>)</p></li> <li><p>Dependency Injection</p> <ul> <li>I've looked at this website, <a href="http://www.zenjava.com/2011/10/23/javafx-2-0-fxml-and-spring/" rel="noreferrer">http://www.zenjava.com/2011/10/23/javafx-2-0-fxml-and-spring/</a>, and i have looked a little into google Guice, but I don't see a way of simplistically giving each JavaFX view/controller the same model object. It seemed like the injection would inject a different model for each view/controller.</li> </ul></li> <li><p>Saving the model object as a public static variable</p> <ul> <li>This is an option but at the moment I don't like the idea of having a public static model so open and available. Obviously, I can make it a private static variable and have getters and setters, but i don't like this idea much either. </li> </ul></li> <li><p>Passing Parameters from Caller to Controller</p> <ul> <li><p>I want each controller to load itself in its constructor, and I want each custom controller to be automatically injected into its parent controller. For example, the card overview tab loads itself like this:</p> <pre><code>public CardOverviewTab() { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("card_overview_tab.fxml")); fxmlLoader.setRoot(content); fxmlLoader.setController(this); try { fxmlLoader.load(); } catch (Exception e) { e.printStackTrace(); } } </code></pre></li> <li><p>And the SingleGameSetup controller has the card overview tab automatically injected into a variable:</p> <pre><code>public class SingleGameSetupController extends AnchorPane { @FXML private CardOverviewTab cardOverviewTab; // Rest of the class } </code></pre></li> <li><p>And the part of the fxml containing the card overview tab looks like this:</p> <pre><code>&lt;CardOverviewTab fx:id="cardOverviewTab" /&gt; </code></pre></li> <li><p>This way I do not need to worry about manually loading a controller, but I still have the problem of setting the model.</p></li> </ul></li> <li><p>Setting a Controller on the FXMLLoader</p> <ul> <li>This option is similar to what I am accustom to, passing the model as a parameter into the constructor, but it still has the problem of loading controllers manually with the FXMLLoader. </li> </ul></li> <li><p>Event Bus</p> <ul> <li>I haven't read too much into this, but from what I have read the event bus seems to be inactive and outdated.</li> </ul></li> <li><p>Singleton</p> <ul> <li>This is similar to having a public static reference to the model object that controllers can retrieve, but again I am looking for a better solution. Plus, I do not want a singleton model.</li> </ul></li> </ul> <p><strong>What I am Looking for</strong></p> <p>Is there a way to pass the model object around in a less cumbersome way? I am looking for a way that is as simple as passing the model to a constructor, but I do not want to deal with manually loading controllers via the FXMLLoader, or setting the model after the controllers are loaded. Maybe having a class to retrieve the model is the best option, but I am asking just in case there is a better way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload