Nicholas Ayala
Nicholas Ayala

Reputation: 1

How do I transfer data from one scene to another in JavaFx?

JavaFX

I am creating my first GUI using JavaFX and I am having a problem grabbing user input from two text fields from my first scene and transferring it to the next scene. I am asking for the Users name and wanting to place their input into the label of the very next scene after they press submit. .getText() does not seem to be working since it says that my variables are NULL. I am sure it is something pretty simple I am just overlooking it or thinking to hard about it. Here is the sample code of my application.

public class TitleScreen extends Application {
Stage window;
Button button;
Button clearButton;
Button bButton, vButton, dButton;
Button backButton, completeButton;
Scene scene, scene1, scene2, scene3;
String first, last;
public static void main(String[] args){
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("Flight Assistant");

    /*window.setOnCloseRequest(e -> {
        e.consume();
        closeProgram();
    });*/

    /*
    First scene will ask the User for their name to take that input and place it in the Title
     */
    //Creating a GridPane container
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(5);
    grid.setHgap(5);

    //Defining the Name text field
    final TextField firstName = new TextField();
    firstName.setPromptText("Enter your first name.");
    firstName.setPrefColumnCount(10);
    first = firstName.getText();
    GridPane.setConstraints(firstName, 0, 0);
    grid.getChildren().add(firstName);

    //Defining the Last Name text field
    final TextField lastName = new TextField();
    lastName.setPromptText("Enter your last name.");
    last = lastName.getText();
    GridPane.setConstraints(lastName, 0, 1);
    grid.getChildren().add(lastName);

    //Defining the Submit button
    Button submit = new Button("Submit");
    submit.setOnAction(e-> window.setScene(scene1));
    GridPane.setConstraints(submit, 1, 0);
    grid.getChildren().add(submit);

    //Defining the Clear button
    clearButton = new Button("Clear");
    GridPane.setConstraints(clearButton, 1, 1);
    grid.getChildren().add(clearButton);

    scene = new Scene(grid, 300, 150);

    //Title of scene1
    Label title = new Label();
    title.setText("Welcome " + first + " " + last +" to Flight Assistance!");
    title.setFont(Font.font("Times New Roman", 38));

    //button showing pending request
    button = new Button();
    button.setText("Pending request");
    //switch to scene 2
    button.setOnAction(e -> window.setScene(scene2));

    //Layout 1 and adding elements to the layout
    VBox layout1 = new VBox(200);
    layout1.getChildren().addAll(title, button);
    layout1.setAlignment(Pos.CENTER);

    //setting the layout size for the first scene
    scene1 = new Scene(layout1, 900, 650);

I have also tried placing the String first = firstName.getText(); inside the event handler for the button but it gives me an error that the var has never been used.

Upvotes: 0

Views: 5729

Answers (1)

Gaali Prabhakar
Gaali Prabhakar

Reputation: 583

Your creating second scene on load && and getting values too, two things you can do. 1) you can create scene onSubmit and get values set to fields 2) create scene on load and but set values on submit and set it label in submit

First solution

public class TitleScreen extends Application {
Stage window;
Button button;
Button clearButton;
Button bButton, vButton, dButton;
Button backButton, completeButton;
Scene scene, scene1, scene2, scene3;
String first, last;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("Flight Assistant");

    /*
     * window.setOnCloseRequest(e -> { e.consume(); closeProgram(); });
     */

    /*
     * First scene will ask the User for their name to take that input and
     * place it in the Title
     */
    // Creating a GridPane container
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(5);
    grid.setHgap(5);

    // Defining the Name text field
    final TextField firstName = new TextField();
    firstName.setPromptText("Enter your first name.");
    firstName.setPrefColumnCount(10);

    GridPane.setConstraints(firstName, 0, 0);
    grid.getChildren().add(firstName);

    // Defining the Last Name text field
    final TextField lastName = new TextField();
    lastName.setPromptText("Enter your last name.");

    GridPane.setConstraints(lastName, 0, 1);
    grid.getChildren().add(lastName);

    // Defining the Submit button
    Button submit = new Button("Submit");
    submit.setOnAction(e -> {
        first = firstName.getText();
        last = lastName.getText();
        createNewScene();
    });
    GridPane.setConstraints(submit, 1, 0);
    grid.getChildren().add(submit);

    // Defining the Clear button
    clearButton = new Button("Clear");
    GridPane.setConstraints(clearButton, 1, 1);
    grid.getChildren().add(clearButton);
    scene = new Scene(grid, 300, 150);
    primaryStage.setScene(scene);
    primaryStage.show();

}

public void createNewScene() {
    Label title = new Label();
    title.setText("Welcome " + first + " " + last + " to Flight Assistance!");
    title.setFont(Font.font("Times New Roman", 38));

    // button showing pending request
    button = new Button();
    button.setText("Pending request");
    // switch to scene 2
    button.setOnAction(e -> window.setScene(scene2));

    // Layout 1 and adding elements to the layout
    VBox layout1 = new VBox(200);
    layout1.getChildren().addAll(title, button);
    layout1.setAlignment(Pos.CENTER);

    // setting the layout size for the first scene
    scene1 = new Scene(layout1, 900, 650);
    window.setScene(scene1);
 }
 }

Second solution

public class ThreeStages extends Application {
Stage window;
Button button;
Button clearButton;
Button bButton, vButton, dButton;
Button backButton, completeButton;
Scene scene, scene1, scene2, scene3;
String first, last;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("Flight Assistant");

    /*
     * window.setOnCloseRequest(e -> { e.consume(); closeProgram(); });
     */

    /*
     * First scene will ask the User for their name to take that input and
     * place it in the Title
     */
    // Creating a GridPane container
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(5);
    grid.setHgap(5);

    // Defining the Name text field
    final TextField firstName = new TextField();
    firstName.setPromptText("Enter your first name.");
    firstName.setPrefColumnCount(10);

    GridPane.setConstraints(firstName, 0, 0);
    grid.getChildren().add(firstName);

    // Defining the Last Name text field
    final TextField lastName = new TextField();
    lastName.setPromptText("Enter your last name.");

    GridPane.setConstraints(lastName, 0, 1);
    grid.getChildren().add(lastName);

    // Defining the Submit button
    Button submit = new Button("Submit");
    Label title = new Label();
    submit.setOnAction(e -> {
        first = firstName.getText();
        last = lastName.getText();
        title.setText("Welcome " + first + " " + last + " to Flight Assistance!");
        window.setScene(scene1);
    });
    GridPane.setConstraints(submit, 1, 0);
    grid.getChildren().add(submit);

    // Defining the Clear button
    clearButton = new Button("Clear");
    GridPane.setConstraints(clearButton, 1, 1);
    grid.getChildren().add(clearButton);

    scene = new Scene(grid, 300, 150);
    primaryStage.setScene(scene);
    primaryStage.show();
    // Title of scene1

    title.setFont(Font.font("Times New Roman", 38));

    // button showing pending request
    button = new Button();
    button.setText("Pending request");
    // switch to scene 2
    button.setOnAction(e -> window.setScene(scene2));

    // Layout 1 and adding elements to the layout
    VBox layout1 = new VBox(200);
    layout1.getChildren().addAll(title, button);
    layout1.setAlignment(Pos.CENTER);

    // setting the layout size for the first scene
    scene1 = new Scene(layout1, 900, 650);

  }
}

i hope this will help u. i tested it is working

Upvotes: 1

Related Questions