Pass object from one scene to another

谁都会走 提交于 2019-12-21 20:48:36

问题


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:

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();
    }
}

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 setUserData() 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)

Code at scene1:

//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<Koe> olAfTeWerkenKoeien = DA_Koe.getAfTeWerkenKoeien();
    ss.setScene(primaryStage, "GUI/scenes/koe/Koe.fxml", olAfTeWerkenKoeien);
}

Code at scene2:

public void initialize(URL url, ResourceBundle rb) {
    Scene s = lbl.getScene();
    ObservableList<Koe> olAfTeWerkenKoeien = (ObservableList<Koe>) s.getRoot().getUserData();
    System.out.println(olAfTeWerkenKoeien.size());
} 

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?

When I attach this code to a button on Scene2, it works like a charm, but it should be loaded automatically.

EDIT: The setting of the data with the setMyData() method is not a problem, however retrieving it is:

public ObservableList<Koe> getMyData() {
   return this.myData;
}

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):

@Override
public void initialize(URL url, ResourceBundle rb) {
    ...
    Stage stage = (Stage) btnSluiten.getScene().getWindow();
    CustomScene cs = (CustomScene) stage.getScene();

    ObservableList<Koe> olKoe = cs.getMyData();

    System.out.println(olKoe.size());
}

回答1:


I believe you missed the point within the Scene object. From the Scene class documentation we can see that:

The JavaFX Scene class is the container for all content in a scene graph.

Which means that the Scene object is just a container and as such it's not supposed to hold any data.

With that in mind, you can make another static object with a field such as

private static Label lbl;

...

public static Label getLbl()
{
    return MyStaticObject.Lbl;
}

...

and use it to store your lbl (or whatever object suits your information) and then statically retrieve it.

I'm doing that to set the owner of my other Stage objects from my application. I hope it helps. Cheers




回答2:


If you really want your scene to be meaningful (aka store specific user data) you can extend it:

public class FooScene extends Scene {
   private ObservableList myData;

   public setMyData(ObservableList data) {
       this.myData = data;
       //handle data
   }
}

The easiest way to make sure setup code is called after scene initialized it to call it by yourself:

public class SceneSelector {
    ...
    public void setScene(Stage stage, String fxmlfilename, ObservableList ol) throws Exception{
        String s = "../" + fxmlfilename;
        Parent root = FXMLLoader.load(getClass().getResource(s));

        // first: add root to scene
        FooScene scene = new FooScene(root);
        // second: apply data to scene (or root)
        scene.setMyData(ol);
        stage.setScene(scene);

        //show the stage
        stage.show();
    }
}



回答3:


You can use controller for scenes and pass the data through controller:

String filePath1 =  "../" + fxmlfilename;
URL location1 = YourController1.class.getResource(filePath1);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location1);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
Parent root = (Node) fxmlLoader.load(location1.openStream());

YourController1 ctrl1 = (YourController1) fxmlLoader.getController();

Then you can assign data to the controller:

ctrl1.setUserData();

Finally, just show the scene as you want:

Scene scene = new Scene(root);
stage.setScene(scene);

//show the stage
stage.show();

In the initialize() method in the controller, just get data from controller as usual data object.



来源:https://stackoverflow.com/questions/11508182/pass-object-from-one-scene-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!