Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
How can I make JavaFX re-initialize when Scene is swapped?

I want to make a very simple program in JavaFX. It goes like this:

The user inputs something into a TextField

The program displays the input on a label but on a different Scene


Here is my code:

Controller.java

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import javax.xml.soap.Text;
import java.io.IOException;

public class Controller {

    //textField in sample.fxml
    @FXML
    TextField textField;

    //label in display.fxml
    @FXML
    Label label;

    String text;

    @FXML
    public void enter() throws IOException {
        text = textField.getText();

        Stage stage = (Stage) (textField).getScene().getWindow();

        Parent root = FXMLLoader.load(getClass().getResource("display.fxml"));
        stage.setResizable(false);
        stage.setTitle("Display");
        stage.setScene(new Scene(root, 300, 275));

        label.setText(text);
    }
}

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }

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

There are 2 other FXML files containing either a single textField or a single Label.

But whenever I run this code there is a NullPointerException signaling that label is null, because it hasn't been initialized. How do I fix this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I believe you should make antoher controller for the display.fxml file (the other scene). Than in this new controller you can prepare a function to set label value:

DisplayController.java

public class DisplayController {

    //label in display.fxml
    @FXML
    Label label;

    public void setLabelText(String s){
        label.setText(s);
    }
}

and in Controller.java edit enter() function by calling a new DisplayController.java instance:

    @FXML
    public void enter() throws IOException {
        text = textField.getText();

        Stage stage = (Stage) (textField).getScene().getWindow();
        FXMLLoader loader = new FXMLLoader.load(getClass().getResource("display.fxml"));
        Parent root = loader.load();
        DisplayController dc = loader.getController();
        
        //here call function to set label text
        dc.setLabelText(text);

        stage.setResizable(false);
        stage.setTitle("Display");
        stage.setScene(new Scene(root, 300, 275));

    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!