1

This is my first JavaFX project and i have a problem. How can i keep the score from the firstController to the second? I tried by creating a new object and with getter and setter but i still don't know how to do it. Can someone please help me?

public class firstController extends ActionEvent {
    @FXML
    private Label scoreLabel;
    @FXML
    private Button firstButton;
    public int score;
    private Stage stage;
    private Scene scene;

    public void choose(ActionEvent e){
        if(e.getSource().equals(firstButton)){
            score++;
        }
        scoreLabel.setText("Score: " + score);
    }

    public void next(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("secondScene.fxml"));
        stage = (Stage)((Node)e.getSource()).getScene().getWindow();
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

}



public class secondController {

    @FXML
    private Label scoreLabel;
    @FXML
    private Button secondButton;


    public int score; //here is where i need the help
    private Stage stage;
    private Scene scene;

    public void choose(ActionEvent e){

        if(e.getSource().equals(secondButton)){
            score++;
        }
        scoreLabel.setText("Score: " + score);
    }

    public void next(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("thirdScene.fxml"));
        stage = (Stage)((Node)e.getSource()).getScene().getWindow();
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }
}
MsLaus
  • 21
  • 4

0 Answers0