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/SnowChocobo Mar 23 '25 edited Mar 23 '25
I'm not super sure how to interpret your answer since you argue heavily against coupling, right after you and me agreeing that the FXML part and the FXML controller part being one "unit".
It is also important to distinguish MVC being used in JavaFX itself (MVC being Model/Control, Skin, and Behavior there), and MVC as a general concept of how to structure your application code.
I'm a devout user of "classical" MVVM in the sense that automatic data binding is integral part of MVVM, and not just having something called "ViewModel" and calling it a day.
Again: MVVM as how WPF introduced it, relied heavily on having data binding done by the framework, with a ViewModel only providing observables for the View.
This is also where the loose coupling happens. The ViewModel doesn't care at all how the view renders the data, and the view can be easily swapped by a different implementation.
I don't mind the FXML file being coupled to its FXML controller since they are both the View part anyway. I should have probably mentioned that most of my FXML usages lie in creating custom components (JavaFX terms), i.e. where FXML and FXML controller part are linked together.
How one's application code then uses these custom components is up to one's individual MVC/MVVM direction.
Addendum: Oh and I'm hardly using SceneBuilder too, as it's a hassle to make custom components work with it, and it creates too much boilerplate layout "code." Of course, that might be a proof of what you hint in your other reply, that beginners probably don't know yet what is useful or not. I appreciate you introducing JavaFX concepts to a broader audience, I just think there are more valid ways how to use the tools provided. :)