Object value in class becomes zero inside FXML annotated button action method.
I have an object declared in my class as follows,
private LoginData loginData;
From another Stage I am calling the method switchStage(Stage, LoginData)
to initiate this stage. The method is,
public void switchStage(Stage primaryStage, LoginData lDat) throws Exception{
if(lDat!= null) {
loginData = lDat;
System.out.println("SwitchStage : " + lDat.getUserAccessType());
} else {
loginData = new LoginData();
}
start(primaryStage);
}
In the start(Stage) method, I am loading the stage from an FXML file. The method definition is as follows,
public void start(Stage primaryStage) throws Exception {
System.out.println("Before FXML load : " + loginData.getUserAccessType());
Parent root = FXMLLoader.load(getClass().getResource("/com/<package>/view/LoadFiles.fxml"));
System.out.println("After FXML Load : " + loginData.getUserAccessType());
Scene scene = new Scene(root);
root.setStyle("-fx-border-color: black; -fx-border-width: 1px; ");
if (!primaryStage.isShowing())
primaryStage.initStyle(StageStyle.UNDECORATED);
scene.getStylesheets().add(ReconcileinaLoadFilesController.class.getResource("/com/<package>/view/LoadFiles.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
System.out.println("Before Stage show : " + loginData.getUserAccessType());
primaryStage.show();
System.out.println("After Stage show : " + loginData.getUserAccessType());
}
I can see the value being retained in each of the above println
statements.
I have a ToggleButton in my FXML file and the action method definition (in same class as the above methods) is as follows,
@FXML
public void settingsBtnAction() throws IOException{
if(loginData != null) {
System.out.println("Settings: " + loginData.getUserAccessType());
} else {
System.out.println("loginData is null"); // <--- code returns this everytime.
}
}
What is happening here and how do I retain the value of the object loginData
in the ToggleButton's action method? I am completely lost.
Edit:
I tried initializing the object in the line of declaration and it looks like it is retaining the value from here, but its not able to get the value that I've set manually.
private LoginData loginData = new LoginData(); //Gets me the value set in the constructor of the LoginData class.