Note that there are some explanatory texts on larger screens.

plurals
  1. POPass object from one scene to another
    primarykey
    data
    text
    <p>As I'm learning the new world of JavaFX2 I stumbled on another annoying problem. I'm developing a program with multiple scenes (~10 scenes). For that I created a small class like this:</p> <pre><code>public class SceneSelector { ... public void setScene(Stage stage, String fxmlfilename, ObservableList ol) throws Exception{ String s = "../" + fxmlfilename; Parent root = FXMLLoader.load(getClass().getResource(s)); root.setUserData(ol); Scene scene = new Scene(root); stage.setScene(scene); //show the stage stage.show(); } } </code></pre> <p>This class works good enough for switching between the scenes. Now the problem is that I sometimes need to pass data from Scene1 to Scene2. I'm trying to do this by setting the <code>setUserData()</code> for the new scene which basicly works exept for one thing. How can I get the userdata when the new Scene is beeing initialized? (because the Nodes are still null at that time)</p> <p>Code at scene1:</p> <pre><code>//Code connected to a button that opens the new Scene private void openLabID(ActionEvent event) throws Exception { final Stage primaryStage = (Stage) btnNewScene.getScene().getWindow(); ObservableList&lt;Koe&gt; olAfTeWerkenKoeien = DA_Koe.getAfTeWerkenKoeien(); ss.setScene(primaryStage, "GUI/scenes/koe/Koe.fxml", olAfTeWerkenKoeien); } </code></pre> <p>Code at scene2:</p> <pre><code>public void initialize(URL url, ResourceBundle rb) { Scene s = lbl.getScene(); ObservableList&lt;Koe&gt; olAfTeWerkenKoeien = (ObservableList&lt;Koe&gt;) s.getRoot().getUserData(); System.out.println(olAfTeWerkenKoeien.size()); } </code></pre> <p>Of course Scene s gives a null value at this point (because lbl is null at this point), so I wonder, is there a method that is beeing fired right after initialize?</p> <p>When I attach this code to a button on Scene2, it works like a charm, but it should be loaded automatically.</p> <p>EDIT: The setting of the data with the setMyData() method is not a problem, however retrieving it is:</p> <pre><code>public ObservableList&lt;Koe&gt; getMyData() { return this.myData; } </code></pre> <p>How can I get the CustomScene object when a controller initializes? Because doing this below will result in a NullPointerException (because btnSluiten is not initialized just yet):</p> <pre><code>@Override public void initialize(URL url, ResourceBundle rb) { ... Stage stage = (Stage) btnSluiten.getScene().getWindow(); CustomScene cs = (CustomScene) stage.getScene(); ObservableList&lt;Koe&gt; olKoe = cs.getMyData(); System.out.println(olKoe.size()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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