Ido Gal
Ido Gal

Reputation: 527

JavaFX - Passing values between windows (stages)

I have a JavaFX application in which I have a main window (stage) from which the user should open a second, child, window. In the second window the use should do some things which will affect the application behavior later on.

I couldn't really understand how should I access the second screen's data/values after the user closes this screen.

This is an example of a similar situation. Let's say I want to get the second stage's text box value.

package multiplestagesexample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 *
 * @author IdoG
 */
public class MultipleStagesExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("New Stage");        
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                new CreateStage();
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Main Stage");
        primaryStage.setScene(scene);
        primaryStage.show();

        // Get the TextField value from the Additional Stage ???
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }    
}

class CreateStage {
    public CreateStage() {
        TextField textBox = new TextField();        

        StackPane root = new StackPane();
        root.getChildren().add(textBox);        

        Scene scene = new Scene(root, 300, 250);

        Stage stage = new Stage();
        stage.setTitle("Additional Stage");
        stage.setScene(scene);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.showAndWait();
    }
}

This is a section of the relevant "real" code:

@FXML
private void handleOpenMappingToolWindowAction(ActionEvent event) throws IOException {
    //mappingWindow is an instance variable
    //Stage mappingWindow; 

    // gets a Sheet object that will be used in the "MappingToolWindow" stage
    selectedSheet = (Sheet) sheetsBox.getSelectionModel().getSelectedItem(); 

    FXMLLoader loader = new FXMLLoader(getClass().getResource("MappingToolWindow.fxml"));
    Parent root = (Parent)loader.load();
    MappingToolWindowController controller = loader.getController();

    if (mappingWindow == null) {
        Scene mappingScene = new Scene(root);
        mappingWindow = new Stage();

        mappingWindow.initModality(Modality.APPLICATION_MODAL);
        mappingWindow.setTitle("Mapping Tool");
        mappingWindow.setScene(mappingScene);      

        controller.setSheet(selectedSheet);
    }

    mappingWindow.showAndWait();

    // ACCESS THE NEW STAGE ???
}

Upvotes: 4

Views: 9283

Answers (2)

rizcan
rizcan

Reputation: 57

public class XXXX extends Window {
    private static Stage popupstage;
    private Person aperson;
    public XXXX (Person a) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("../UserInterface/UI_XXXXX.fxml"));
        popupstage = new Stage();
        popupstage.setTitle(a.getAbc());
        popupstage.initModality(Modality.APPLICATION_MODAL);
        popupstage.setScene(new Scene(root,400,400));
    }
    public Stage getPopupstage() {
        return popupstage;
    }
}

Have a window that takes a parameter. Your window will then read the object passed. In the above example, the person object is read and it populate the title of the stage (new window).

Upvotes: 0

James_D
James_D

Reputation: 209714

Make the text field an instance variable, so that you can define a method to access its text:

class CreateStage {

    private TextField textBox ;

    public CreateStage() {
        textBox = new TextField();        

        StackPane root = new StackPane();
        root.getChildren().add(textBox);        

        Scene scene = new Scene(root, 300, 250);

        Stage stage = new Stage();
        stage.setTitle("Additional Stage");
        stage.setScene(scene);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.showAndWait();
    }

    public String getText() {
        return textBox.getText();
    }
}

Now of course you can just do

CreateStage dialog = new CreateStage() ;
System.out.println(dialog.getText()); 

in your handler.

Upvotes: 4

Related Questions