Skip to content
Advertisement

How can I assign a value to my GUI attributes from a custom object? [closed]

I am trying to make a game similar to bitlife, and in my program the first scene is the main menu where it asks the user to enter firstname (textbox), lastname (textbox) and choose a gender (choicebox). After the user clicks new game (button), the scene switches to the actual game scene. In this scene, I want to display the name of the sim (character the user created), and randomly assigned happiness, health, smarts and looks bars (I made them a progress bar). How can I display these assigned values in the game scene? I am using FXML files to create the user interface for the game. Also, i want to note that all stages have their own controllers. So main menu and game scene has different controllers.
In the previous (first) scene’s controller class, which is the game menu scene, after the user clicks new game button, the handler creates the sim like this:

        Sim.firstname = firstname.getText();
        Sim.lastname = lastname.getText();
        Sim.gender = genders.getValue();
        Sim.happiness = random.nextFloat();
        Sim.health = random.nextFloat();
        Sim.smarts = random.nextFloat();
        Sim.looks = random.nextFloat(); //0.0-1.0

I have also attached a picture of what the game interface will look like. enter image description here

Advertisement

Answer

I solved it like this in the Controller class:

 @Override
    public void initialize(URL location, ResourceBundle resources) {
        fullname.setText(Sim.firstname + " " + Sim.lastname);
        bank_balance.setText(Integer.toString(Sim.bankbalance));
        happiness.setProgress(Sim.happiness);
        health.setProgress(Sim.health);
        smarts.setProgress(Sim.smarts);
        looks.setProgress(Sim.looks);
    }

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement