5

This should be a fairly simple question, but I can't seem to find a good answer on the internet.

I've been looking into Java FX and i find it interesting, although I'm having troubles being content with how I manage to separate the logic from the presentation of my program.

Notice: This might be a possible duplicate
Javafx 2.0 How-to Application.getParameters() in a Controller.java file
However the suggestion in the accepted answer there is to make the variable static, and I do not wish to do that either.

Background

All the tutorials I've found has a controller which is supposed to be the connection between the presentation and the logic in good'ol MVC manner. However it's about here that I find myself stumbling, because I wan't to initialize a some stuff, what isn't important in this case, and pass it in to my Controller.

The Question

Am I supposed to want to pass stuff to my controller or should it be responsible for initializing (either by itself or by instantiating another object which has that responsibility)?

TL;DR

I want to pass stuff to my controller in Java FX, should I do that or is it a bad practice? If I should pass stuff, how do I do that?

Some code to clarify:

Heres is how I don't want to do:

The main class

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("ChatGUI.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

And the example controller (with comments)

@Override
public void initialize(URL url, ResourceBundle rb) {
    //Intialize a bunch of stuff here.
    connectionManager = new MyConnectionManager();    
}
And something like how I would like to do:

The main class

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("ChatGUI.fxml"));
    MyConnectionManager connectionManager = new MyConnectionManager();
    root.addConnectionManager(connectionManager);

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

And the example controller (with comments)

@Override
public void initialize(URL url, ResourceBundle rb) {
    //Intialize a bunch of stuff here.
    connectionManager = new MyConnectionManager();    
}

public void addConnectionManager(ConnectionManager manager) {
    this->myManager = manager; 
} 

I want to achieve something similar to the second example above, is this possible or do I have some fundamental missunderstanding of the MVC pattern?

Community
  • 1
  • 1
Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66

0 Answers0