Skip to content
Advertisement

How to access a variable from one class controller file to another in JavaFX?

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

JavaScript

GameStageController.java

JavaScript

Advertisement

Answer

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

JavaScript

and then of course

JavaScript

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.

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