0

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

Community
  • 1
  • 1
kaikun
  • 98
  • 10
  • There's no difference other than the additional code you are calling in the `Admin` constructor and possibly in the`showWelcomeMessage` method. Presumably you are not getting an exception. Try creating a minimal example with this structure and see if it works. – James_D Sep 29 '16 at 10:13
  • The showWelcomeMessage simply sets the text visible, even though I left it visible from the beginning for the tests. public void showWelcomeMessage() { WelcomeText.setVisible(true); } The constructor is posted. Yes there is no exception, just the Pane is empty. – kaikun Sep 29 '16 at 10:41
  • Comment out the additional code in the constructor and see if it makes a difference. It should work. Create a [MCVE] and [edit] your question to include it if you can't find the actual source of the problem. There is nothing wrong with the code you have posted. – James_D Sep 29 '16 at 10:47
  • Just to update; I tried the exact code you posted, without the dao call and with the simplest implementations of the missing pieces I could do and it worked fine. So there is not enough information in your question as it stands for anyone to provide an answer. – James_D Sep 29 '16 at 14:01

0 Answers0