Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple FXML with Controllers, share object
    primarykey
    data
    text
    <p>Goodevening everyone,</p> <p>I have found a bunch of posts already on this topic but I still can not manage to pass an object from Controller1 to Controller2. Is there somewhere a full tutorial or some example project that does this?</p> <p>I've gotten this far until I got stuck:</p> <p><b>Country class</b></p> <pre><code>public class Country { private SimpleStringProperty country = new SimpleStringProperty(""); //Constructor public Country() { } //GETTERS public String getCountry() { return country.get(); } //SETTERS public void setCountry(String value) { country.set(value); } @Override public String toString() { return getCountry(); } } </code></pre> <p>When the program starts, the main FXML gets loaded (Sample.fxml). This contains a border pane with a menu bar in the top panel and a content pane in the center. On initialize I create a new Country object and store it in a global variable. I have a method that loads another FXML into the content pane when a menu item is clicked:</p> <p><b>SampleController.java</b></p> <pre><code>public class SampleController implements Initializable { @FXML private Pane pContent; private Country c; @FXML private void handleButtonAction(ActionEvent event) throws IOException { System.out.println(c); //this prints Belgium, which is correct 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())); } @Override public void initialize(URL url, ResourceBundle rb) { c = new Country(); c.setCountry("Belgium"); } public Country getCountryFromSampleController(){ return c; } } </code></pre> <p>Now I wish to capture the Country object when the Sub1.fxml gets loaded, which means I need to fetch the country object on initialize():</p> <p><b>Sub1Controller.java</b></p> <pre><code>public class Sub1Controller implements Initializable { /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { SampleController sp = new SampleController(); //I don't know how to fetch the original SampleController object System.out.println(sp.getCountryFromSampleController()); //this prints null, which is ofcourse logical because I make a new SampleController object. } } </code></pre> <p>The question that I have, how can I get the 'original' SampleController object so I can use the getCountryFromRoot() method to fetch the Country object with value Belgium? I've been searching on this issue for hours and hours and have read every post on StackOverflow about this, but it seems I do not find the missing link... any help (preferably with this code) is appreciated!</p> <p>Sorry for the long post, I tried to be as complete as possible else I'll never understand...</p>
    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.
 

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