r/JavaFX • u/hamsterrage1 • Mar 21 '25
Tutorial New Article: Should You Use FXML?
This topic has come up here a few times recently, and I've had a few DM's about it too. I had the feeling that I must have covered this topic over and over, but when I looked back through my articles I only found one from around 2021 that talked about FXML and MVC.
This ended up being a longer article than I anticipated, and Jekyll says it's an even longer read because I included 462 lines of FXML that I scooped off GitHub to make a point about how "intuitively readable" it isn't. But it's still long.
So, if you want the TDLR, here it is:
Most of the wondrous claims about how FXML instantly improves your various aspects of your application design are just rubbish - and I take some time to prove it for a few of them. It's clear that even some of the claims made in the Oracle tutorials are just wrong.
What you do get from FXML is the ability to SceneBuilder, and that should be the sole motivation for your decision to use FXML - which is actually backwards: SceneBuilder is the only motivation to use FXML. I'm also fairly certain that SceneBuilder isn't a good beginners' tool either.
The article explores how it's tougher to employ a library of custom methods and classes to simplify layout creation with FXML.
Finally, I take a look how to properly integrate FXML with a framework. In this case I use MVCI (because it's better, of course). This is probably the most important section for any of you determined to use FXML but still want to architect your applications properly, because 99% of the tutorials out there on the web just get this wrong.
If any of that intrigues you, and you want to know more, then have a look at the article:
View all comments
Show parent comment
1
u/hamsterrage1 Mar 23 '25 edited Mar 23 '25
First, the context of View in the article was with regards to MVC/MVVM/MVCI, and I stand behind those two components comprising the View together.
But yes, the FXML file and FXML Controller are clearly two separate things. But then I worry about coupling. Everyone should worry about coupling...all the time.
That's because coupling determines how much a change to one component ripples through your system. A change to the internal implementation in one class requires a change to the internal implementation of another class, and so on, and so on...
Clearly these two components have to be tightly coupled. In most implementations, this is two ways. For instance, you cannot change the fxid of an element without changing the FXML Controller also. You also cannot change the name of an EventHandler in the FXML Controller without also changing the FXML file.
In the article I do point out that you could create an FXML file with no dependencies on the FXML Controller, but this would mean sacrificing some of the capabilities of FXML. And you rarely see this done in practice.
What I didn't talk about in the article is that, in an MVC/MVCI context, the FXML Controller is essentially the interface between the View and the Controller. If you can make the FXML file uncoupled from the implementation of the FXML Controller, then you can implement the "Adapter Pattern", with the FXML Controller as the adapter. To me, this would be the optimal way to integrate FXML with a framework. Now, your FXML file is isolated from the rest of the framework and it's not dependant on anything. This means that you can modify implementation of the FXML Controller, the MVC Controller, and the Model (or Model and Interactor) without any impact on the FXML file.
The things that you suggest, that make FXML more powerful all work the other way and actually more tightly coupled the FXML file the the FXML Controller. In fact, when you make the bindings into the FXML file, as you suggest, you actually reach through the FXML Controller to couple the FXML file to the Model.
I admit to being a fanatic about it, but I consider coupling to be the absolute top consideration when making design choices. And that's why I think that FXML is a bit of a trap for beginners: On one hand, there are all of these claims about how it automatically improves your design through things like "separation of concerns" that you get for free. On the other hand, if you use FXML the way that it is clearly intended to be used, you inevitably end up creating a mountain of coupling that should be trying to avoid.