3 Replies Latest reply: Jun 25, 2013 11:45 AM by 78b23d33-c347-401e-8a7d-abdf360732be RSS

    How to pass an objeto to a stage


      I need to transfer an object from a stage to another one and I don't know how to do that, someone could help me ?


      Thanks in advance!

        • 1. Re: How to pass an objeto to a stage

          You may need to make your question more specific. The usual answer to these type of questions is to create the object somewhere else and give both stages access to it. Here's an example where the primary stage has a label and a dialog stage has a text field which both access the same data, held in a StringProperty:


          import javafx.application.Application;
          import javafx.beans.binding.Bindings;
          import javafx.beans.property.SimpleStringProperty;
          import javafx.beans.property.StringProperty;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.geometry.Insets;
          import javafx.geometry.Pos;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.control.Label;
          import javafx.scene.control.TextField;
          import javafx.scene.layout.HBox;
          import javafx.scene.layout.VBox;
          import javafx.stage.Modality;
          import javafx.stage.Stage;
          import javafx.stage.StageStyle;
          public class SimpleSharedData extends Application {
            public void start(final Stage primaryStage) {
            final StringProperty sharedData = new SimpleStringProperty();
            final VBox root = new VBox(5);
            root.setPadding(new Insets(5));
            Label label = new Label();
            label.textProperty().bind(Bindings.concat("Current value: ", sharedData));
            Button changeButton = new Button("Change...");
            changeButton.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
                  Scene dialogScene = createDialogScene(sharedData);
                  Stage dialog = new Stage(StageStyle.UNDECORATED);
            root.getChildren().addAll(label, changeButton);
            Scene scene = new Scene(root, 200, 300);
            private Scene createDialogScene(final StringProperty data) {
              final VBox root = new VBox(5);
              root.setPadding(new Insets(5));
              final Scene scene = new Scene(root, 300, 200);
              final HBox entry = new HBox(5);
              entry.setPadding(new Insets(5));
              final TextField dataEntry = new TextField(data.get());
              entry.getChildren().addAll(new Label("New value:"), dataEntry);
              final HBox buttons = new HBox(5);
              buttons.setPadding(new Insets(5));
              final Button okButton = new Button("OK");
              okButton.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
              final Button cancelButton = new Button("Cancel");
              cancelButton.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
              buttons.getChildren().addAll(okButton, cancelButton);
              root.getChildren().addAll(entry, buttons);
              return scene ;
            public static void main(String[] args) {
          • 2. Re: How to pass an objeto to a stage

            Why don't you use some kind of Class which contains the static data.


            class ListOfObjects{
                 private static Object saveObject;
                 public Object getObject(){
                      return saveObject;
                 public void setObject(Object o){
                      saveObject = o;
            //STAGE 1
            ListOfObjects mem = new ListOfObjects ();
            mem.setObject("Whew my first saving of data");
            //STAGE N
            ListOfObjects mem = new ListOfObjects ();
            System.out.println(mem.getObject()); //Prints the latest saved object.



            • 3. Re: How to pass an objeto to a stage

              Thanks Narayan! It`s a perfect solution for my problem!