0

In my program I want to update my model from sliders, and it does so like this

public class Controller implements Initializable {

    @FXML
    private Model model = new Model();
    @FXML
    public void editDone() throws Exception {
        model.setStarterCash((int)cashSlider.getValue());
        model.setNumAction((int)acSlider.getValue());
        model.setNumSalary((int)scSlider.getValue());
        model.setNumCareer((int)ccSlider.getValue());


        Stage doneStage = (Stage)editSave.getScene().getWindow();
        Parent doneView = FXMLLoader.load(getClass().getResource("View/editMenu.fxml"));

        Scene doneScene = new Scene(doneView);
        doneStage.setScene(doneScene);
        doneStage.show();

        System.out.println("Starter cash set to: " + model.getStarterCash());
        System.out.println("Action Deck amount set to: " + model.getNumAction());
        System.out.println("Salary Deck amount set to: " + model.getNumSalary());
        System.out.println("Career Deck amount set to: " + model.getNumCareer());
    }
}

Which prints it out into the console

Starter cash set to: 350000
Action Deck amount set to: 75
Salary Deck amount set to: 20
Career Deck amount set to: 11

But when the scene changes and I try to check my if my values are still the same in another scene (with getters), it get reset/displays 0. Am I properly updating my model correctly?

UPDATE This is the model class. The model contains setters and getters for data from the an "options" scene but when I try access the values I've set from said scene in a different scene, say "start game" scene, it outputs 0.

package Model;

public class Model {
    private Board b;

    private int starterCash;
    private int numAction;
    private int numSalary;
    private int numCareer;
    private int numPlayers;

    public Model () {
         b = new Board();
    }

    public int getStarterCash() {
        return starterCash;
    }

    public int getNumAction() {
        return numAction;
    }

    public int getNumCareer() {
        return numCareer;
    }

    public int getNumSalary() {
        return numSalary;
    }

    public int getNumPlayers() {
        return numPlayers;
    }

    public void setNumPlayers(int numPlayers) {
        this.numPlayers = numPlayers;
    }

    public void setStarterCash(int starterCash) {
        this.starterCash = starterCash;
    }

    public void setNumAction(int numAction) {
        this.numAction = numAction;
    }

    public void setNumCareer(int numCareer) {
        this.numCareer = numCareer;
    }

    public void setNumSalary(int numSalary) {
        this.numSalary = numSalary;
    }

    public Board getB() {
        return b;
    }

Here's the Main class where the application starts, if it is needed.

package View;

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("mainMenu.fxml"));
        primaryStage.setTitle("That's Life!");
        primaryStage.setScene(new Scene(root, 1024, 576));
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
sphynxo
  • 41
  • 2
  • 5
  • 1
    What is `model`? How is it declared? How is it initialized? --- In another scene? Does each scene perhaps have their own different instance of `model`? – Andreas Sep 24 '20 at 09:49
  • Also, I only instantiated the model class once in Controller. – sphynxo Sep 24 '20 at 10:02
  • Does this answer your question? [Is @FXML needed for every declaration?](https://stackoverflow.com/q/30210170/5221149) – Andreas Sep 24 '20 at 10:08
  • Basically, `@FXML` conflicts with `= new Model()`, so which of the two did you actually intend to have? My guess is the `new` call, so remove the `@FXML` annotation, unless of course you did want the field initialized from the FXML file. – Andreas Sep 24 '20 at 10:10
  • @Andreas. If the value is initialized inline, and there's no value defined in the FXML file with that `fx:id`, the `@FXML` annotation will simply have no effect. It's not causing the problem. – James_D Sep 24 '20 at 12:30
  • 1
    You create a new model instance every time a new controller instance is created. So if you reload the FXML, you get a new model. – James_D Sep 24 '20 at 12:31

0 Answers0