I implemented a simple application with the MVC-Pattern and used a console for the output. Now I tried to replace the console with a simple JavaFX-Approach to test the independence of my design.
The whole logic is covered in the Controller class Admin. It is has an object a_view which gets initialized at creation and implements the IView interface, which has a showWelcomeText() function.
I initialize the Scene and the controller in Main.java:
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
Parent root = fxmlLoader.load(getClass().getResource("/view/DesktopApp.fxml").openStream());
Scene scene = new Scene(root,800,500);
primaryStage.setScene(scene);
primaryStage.show();
IView a_view = (IView) fxmlLoader.getController(); // JavaFXGUI class connected to the root
//a_view.showWelcomeMessage(); // shows message
Admin secretary = new Admin(a_view);
secretary.manage(); // shows empty Form
} catch(Exception e) {
e.printStackTrace();
}
}
The JavaFXGUI-controller is passed as parameter and then initialized in the admin class.
public Admin(IView a_view){
this.a_view = a_view;
md_list = dao.MembersDAO.jaxbXMLToObject(); // read out of XML
}
public void manage(){
a_view.showWelcomeMessage();
However when I run the program it just shows me an empty form and does not display the welcome text. If I comment out the call of manage() and call a_view.showWelcomeMessage() directly it works fine and the message is shown in the form.
I thought it could be a problem to pass the variable(a_view) as parameter (e.g. Java does not pass reference), so I also tried to declare it as static public in the Admin class. It results in the same even both calls refer to the same object.
I searched a lot and amongst other stuff I saw this thread about how to access the controller Accessing FXML controller class but couldn't make it work with the calls from the original controller class. Where is the difference between calling the public static (or private and pass as parameter) and calling it from the start method?
Kind regards