3
votes

I'm trying to change a text in a label, a text which was entered in a text field in a different scene. I made 2 FXML files, the first one contains a textfield and "ok" button, the second one contains a label(with the text "Label"). My goal is to enter a text in the textfield, and when I press "ok"-> open the new scene and the label will change it's text to the text I entered in the text field. I easily changed the label text when the label, the textfield and the ok button were all in the same scene, but when I do it while opening a new scene I fail... After some research, I made a controller for each FXML file, and a "MainController" that will communicate between them. This is my main class:

public class MainBanana extends Application {


@Override
public void start(Stage primaryStage) throws IOException {

    Parent root = FXMLLoader.load(getClass().getResource("view/Welcome.fxml"));
    Scene scene = new Scene(root);
    primaryStage.setTitle("MokaApp");
    primaryStage.setScene(scene);
    primaryStage.show();
    primaryStage.setResizable(false);



}

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

}

}

my first scene controller:

public class WelcomeController {

@FXML
public TextField nameField;
@FXML
private Button okButton;

private MainController main;


@FXML
public void okClicked(ActionEvent event) throws IOException{



    Parent root = FXMLLoader.load(getClass().getResource("Person.fxml"));
    okButton.getScene().setRoot(root);
    System.out.println(nameField.getText());
    main.setLblFromTf(nameField.getText());


}


    public void init(MainController mainController) {
        main=mainController;

    }

}

second scene controller:

public class PersonController {

@FXML
public Label nameLabel;

private MainController main;


public void init(MainController mainController) {
    main=mainController;

}

}

When I launch the program, The Welcome scene is opened, I enter a text to the textfield, but whenever I press the "ok" button, the scene changes to the second scene, but the label text stays the same(label) and I get a nullpointerexception error on this line(located in WelcomeController): main.setLblFromTf(nameField.getText());

Sorry for the long post..

1

1 Answers

3
votes

You don't need the references to MainController all over the place.

The easiest way is:

public class PersonController {

    @FXML
    private Label nameLabel ;

    public void setName(String name) {
        nameLabel.setText(name);
    }
}

Then you can do

public class WelcomeController {

    @FXML
    private TextField textField ;

    @FXML
    private Button okButton ;

    @FXML
    public void okClicked() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Person.fxml"));
        Parent root = loader.load();
        PersonController personController = loader.getController();
        personController.setName(textField.getText());
        okButton.getScene().setRoot(root);
    }
}