I am working on a Tic Tac Toe game using FXML and SceneBuilder. So far I have a login UI scene and a Game UI scene. Inside the login scene there are 2 textfields where the two players input their names and a start button. When the start button is pressed I store those names into a string so I can use them later. I tested it out, and this is working perfect, I printed the strings and its the strings that were inputted. I store them into strings by doing:
@FXML
Text player1;
@FXML
Text player2;
@FXML
TextField player1text;
@FXML
TextField player2text;
String player1name;
String player2name;
//ignore above this, I'm just showing my Textfield and text names to avoid confusion
player1name = player1text.getText();
player2name = player2text.getText();
My problem is when switching over to the Game scene. When I switch over by pressing the start button, it switches over but instead of updating player names, it goes blank as if the strings (which i just initialized) were empty. I played around with it trying to print the strings in the new scene and it says the strings are (null)...why is this? Does Java clear memory when switching scenes? Or what? Is there any way around this? Thank you in advance. I will attach my code below.
Here is my HandleStartButton function that switches scenes to the Game scene and updates the strings when the start button is pressed in the Login Scene:
@FXML
private void handleStartButtonAction(ActionEvent event) throws IOException
{
Stage stage;
Parent root;
//stores words in player1text field and player2text fields in Strings
player1name = player1text.getText();
player2name = player2text.getText();
if (event.getSource() == StartButton)
{
//create new stage
stage = new Stage();
//load up FXML doc where game is
root = FXMLLoader.load(getClass().getResource("GameUI.fxml"));
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(StartButton.getScene().getWindow());
stage.showAndWait();
}
else
{
stage=(Stage) StartButton.getScene().getWindow();
stage.close();
}
}
Here is my Play function that starts game inside the Game scene when another start button is pressed. This also ENABLES the tiles so they can be pressed and it sets the players names to the strings (which is what isn't working because it says the strings are null)
@FXML
private void play(ActionEvent e)
{
if(e.getSource() == gameButton)
{
//disable start button
gameButton.setDisable(true);
//sets text inside scene builder to strings
player1.setText(player1name)
player2.setText(player2name);
//enable all tic tac toe buttons
TicButton1.setDisable(false);
TicButton2.setDisable(false);
TicButton3.setDisable(false);
TicButton4.setDisable(false);
TicButton5.setDisable(false);
TicButton6.setDisable(false);
TicButton7.setDisable(false);
TicButton8.setDisable(false);
TicButton9.setDisable(false);
//calls playTicTacToe function
playTicTacToe(e);
}
}
Finally, i will add screenshots of how the UI's look in sceneBuilder. Any help will be very much appreciated :) Thanks!