1

So I have an integer number called "size" saved to a controller class called SettingsStageController.java and I want that variable to be accessed through my other controller class file called GameStageController.java but I can't seem to find out how.

SettingsStageController.java

/* has the int size variable stored in this file */
int size = 5;

public void startGame(ActionEvent event) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("gameStage.fxml"));
        root = loader.load();
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); // ti ston poutso
        scene = new Scene(root);
        stage.setTitle("DnB: " + Integer.toString(size) + "x" + Integer.toString(size) + " Game");
        stage.setScene(scene);
        stage.show();

        GameStageController gameStageController = loader.getController();
        gameStageController.showPane();

    }

GameStageController.java

    public class GameStageController implements Initializable {
    
    
    @FXML
    Text testText;

    @FXML
    AnchorPane twoXtwoPane;
    
    @FXML
    AnchorPane threeXthreePane;
    
    @FXML
    AnchorPane fourXfourPane;
    
    @FXML
    AnchorPane fiveXfivePane;
    
    
    public void showPane() {
        switch (/* I WANT TO PUT THE "SIZE" NUMBER HERE" */) {
            case 2:
                twoXtwoPane.setDisable(false);
                twoXtwoPane.setVisible(true);
                break;
            case 3:
                threeXthreePane.setDisable(false);
                threeXthreePane.setVisible(true);
                break;
            case 4:
                fourXfourPane.setDisable(false);
                fourXfourPane.setVisible(true);
                break;
            case 5:
                fiveXfivePane.setDisable(false);
                fiveXfivePane.setVisible(true);
                break;
            default:
                twoXtwoPane.setDisable(false);
                twoXtwoPane.setVisible(true);
                break;
        }
    } 
}

8
  • Where is showPane() being called from? Can’t you just make size a parameter to that method, or just pass the size to the controller when the relevant FXML is loaded? Create and post a minimal reproducible example. Commented May 12, 2022 at 15:03
  • @James_D the showPane() method is being called from my SettingsStageController.java class file but its location is at the GameStageController.java file I edited some of the code if that helps. Commented May 12, 2022 at 15:12
  • 1
    So just define public void showPane(int size) { … } and call showPane(size). It’s not clear what the problem is. Commented May 12, 2022 at 15:14
  • @James_D besides from what the user "Nouredine LAMAMRA" sent me as a solution, this solution also worked. thank you as well! Now I only wonder, which solution should I use and if they have any differences with one another Commented May 12, 2022 at 15:23
  • Which solution conforms to basic good programming principles? Which supports encapsulation better? Which keeps variable scope as small as it needs to be? Which allows for extensibility (e.g. later you want to open two games in different windows)? Commented May 12, 2022 at 15:26

2 Answers 2

4

If a method needs data to perform its functionality, then that data should be a parameter to the method. You should do:

public class GameStageController implements Initializable {

    // ...

    public void showPane(int size) {
        switch (size) {
            // ...
        }
    }
}

and then of course

private int size = 5;

public void startGame(ActionEvent event) throws IOException {

    // ...

    GameStageController gameStageController = loader.getController();
    gameStageController.showPane(size);

}

If your GameStageController instance needs the size variable later on, you can create an instance variable in that class, and set it in the showPane method to the value passed as the parameter.

Sign up to request clarification or add additional context in comments.

3 Comments

so I put for example: int s = size; inside the showPane() method so I can later get the size from the class? Sorry if I'm mistaken, i'm just a little confused.
@dolias Create an instance variable and set it in the showPane method. I don’t really know how to say it any more simply.
alright, I think I got it thank you
-1

You just need to make it static so you can access it using the class name directly and if your controllers are in different packages you need to add public because by default the visibility is package.

So you have to declare size like this :

public static int size = 5;

To access it you do :

SettingsStageController.size

6 Comments

Please don’t use this ridiculous approach. What if you needed two different instances of the UI with different sizes?
that seemed to work as for now, thank you!
This breaks almost every object oriented principle in existence, starting with encapsulation. Please stop encouraging nonsense like this, either by posting it in the first place, or by accepting it as an answer. This is supposed to be a site for professional (and other) programmers, not a repository for any old code that just happens to “work”.
He needed a solution so I gave him one. That's it. About java principles, yes I just forgot to ask if he will needs more than one UI instances. In this case he can create another class for data and have an object from that class because storing attributes like that in controller and use them in another one it's not the best idea but as I said, I gave just a solution but it is not the perfect one.
Sorry, but that attitude ("This code happens to work, so I'm going to use it, even if I know it's not a good idea") is everything that is wrong in the industry these days, and you do everyone a vast disservice by propagating it. Not all my code is perfect, by any stretch of the imagination, but I never knowingly advocate bad ideas.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.