0

From what I understand and from what I've read, MVC in JavaFX has different interpretations and variations. I was previously coding my app in a very disorganized way that used many static variables, lacking proper modularity, so I want to organize it. I've had experience in MVC, just not in JavaFX.

Since I have only one Scene in my app, which is a BorderPane, my view would simply be the different nodes that make up the BorderPane. I have all these nodes already made in SceneBuilder and thus a corresponding FXML file. I only have one FXML file and one Controller.

The center pane would allow for the user to dynamically create different objects, and those objects, such as their quantity and values, would be within the model.

Lastly, my Controller class would be responsible with managing the view changes, such as when the user wants to create new objects, and general interactability. It would also handle communication between the BorderPane regions.

I'm trying to take some inspiration from this post here with how to implement MVC, but one thing I can't figure out are where the store the listeners for these dynamically created objects. For example, my project would have user-created nodes and lines connecting them, which are classes I made. The nodes can be right clicked and dragged, and the thus the line would be dynamically recalculated. I previously attached the listeners to my nodes and lines to classes or objects themselves: either in the constructor or a method that removes/adds listeners/handlers, but I would assume that breaks the MVC pattern and modularity? I also previously had many static variables which again is bad design.

How can I best modularize my code to adhere to MVC, with dynamically created, interactable objects in the view? Which class would handle the events for these objects? Is all responsibility for interaction delegated to the Controller class itself?

Jon
  • 35
  • 4
  • Exactly the same principles apply. If you're using FXML, you're really using MVP ("passive view"), not MVC; so the presenter ("controller") would observe the model and update the view when it changes. Your event handlers (including the drag handlers) would update the model, which would invoke listeners on the model which update the view. Your question is really way too vague and broad for this site; see if you can create an example which poses a specific programming question (even then, this may be more appropriate for https://softwareengineering.stackexchange.com/) – James_D Jul 01 '21 at 14:57
  • I suppose the other point to make here is that if everything is dynamic, and you want to follow MVC, perhaps FXML is not the best way to go. – James_D Jul 01 '21 at 15:09
  • I never heard of MVP but I'll look into it. I'll see if I can make a specific example soon. Much appreciated! – Jon Jul 01 '21 at 20:05
  • Well... it's mentioned in the post you linked – James_D Jul 01 '21 at 20:06

0 Answers0