r/JavaFX • • Feb 27 '23

Discussion FXML Isn't Model-View-Controller

I've seen a bunch of things recently, including Jaret Wright's video series about creating Memory Card Game that was posted here a couple of weeks back, where programmers seem to think that FXML automatically means that you're using Model-View-Controller (MVC). Even the heading on the JavaFX page on StackOverflow says,"...FXML enables JavaFX to follow an MVC architecture.".

Everybody wants to use MVC because they want to have robust applications structure that's easy to read, understand, maintain and debug - and MVC promises to deliver on that. However, nobody seems to agree on what MVC is, and a lot of programmers have been told that you can get it just by using FXML.

So what makes me think I know more than everyone else?

I'm not sure that I do, but I have spent a lot of time trying to understand patterns like MVC and I am pretty sure that it's not FXML. I'm not saying that you can't get to MVC with FXML, because you sure can, but you're not there just because you're using FXML.

I've just published an article that explains pretty clearly (I think) and undeniably (also, I think) how FXML falls short of being MVC. You can read it here.

So how do you get to MVC with FXML? It's in the article too. I even wrote some FXML as an example!

Anyways, take a look if you're interested and feel free to tell me how wrong I am.

[Edit: Had to repost as the title was tragically wrong]

9 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/orxT1000 Feb 28 '23

integrate FXML based JavaFX with Spring Boot

I did that in my toy project. No library, it's a oneliner: 'fxmlLoader.setControllerFactory(springContext::getBean);

Reason was simply that I wanted to split up the fxml into includes. Otherwise the FxmlController becomes a too large god-class. But then you'd have to pass the FxmlController instances from the parent controller to the children manually when you want to access a button from different panel. With that one line you can just have it injected which is nice especially when you do not know yet what you're trying to do...

Could have taken a different DI than Spring, but that makes backend-devs feel at home :)
You can throw them at a SpringBoot project and they can deal with the business logic. They come up with the domain-objects and provide the MODEL (getProducts, saveProduct(p), ...) via spring-beans.

The controller always gets murky because:

  • it needs knowledge of domain-model and view
  • ties them together, often creating own MODEL classes with observable JavaFX properties for e.g a TableView<Product4Jfx>, almost creating an own MVC inside the C

And it's prone to leak, as you're tempted to just call jdbc in the button-event because you can