Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>FXML is a simple form of <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC pattern</a>. FXML file is a view, Controller is obvious, what's missed? The model -- a place where you store all data relative to your current view and, thus, which you can use to share Country data between controllers.</p> <hr> <p><strong>1.</strong> One of the possible approach to introduce model is "context". Let's consider a case, then you have only one model for the whole project so you can have a global context in a form of <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a></p> <pre><code>public class Context { private final static Context instance = new Context(); public static Context getInstance() { return instance; } private Country country = new Country(); public Country currentCountry() { return country; } } </code></pre> <p>Your SampleController will have next changes:</p> <pre><code>@Override public void initialize(URL url, ResourceBundle rb) { Context.getInstance().currentCountry().setCountry("Belgium"); } </code></pre> <p>And <code>SubController1</code> can access it the same way:</p> <pre><code>@Override public void initialize(URL url, ResourceBundle rb) { System.out.println(Context.getInstance().currentCountry().getCountry()); } </code></pre> <hr> <p><strong>2.</strong> Another way is to pass context to <code>SubController1</code> then you load it's xml. It will work better if you don't want to have application global model. So create similar Context class but without instance fields, and:</p> <pre><code>public class Sub1Controller implements Initializable { private Context context; public void setContext(Context context) { this.context = context; // initialize country dependent data here rather then in initialize() } } </code></pre> <p>Setting context in <code>SampleController</code>:</p> <pre><code>Context currentContext = new Context(); @Override public void initialize(URL url, ResourceBundle rb) { currentContext.currentCountry().setCountry("Belgium"); } @FXML private void handleButtonAction(ActionEvent event) throws IOException { URL url = getClass().getResource("Sub1.fxml"); FXMLLoader fxmlloader = new FXMLLoader(); fxmlloader.setLocation(url); fxmlloader.setBuilderFactory(new JavaFXBuilderFactory()); pContent.getChildren().clear(); pContent.getChildren().add((Node) fxmlloader.load(url.openStream())); // here we go ((Sub1Controller)fxmlloader.getController()).setContext(currentContext); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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