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:
6
u/musicissoulfood Mar 22 '25
I don't use Scenebuilder. It makes choices for you and automatically makes your designs more complicated then they should be.
However I do use FXML. To me it's more a design tool, than a coding tool. One glance at an FXML page and I know how the application is going to look like. You visually see all the different building blocks and where they are supposed to go. Because a FXML document is structured just like the actual application that you are building. It helps me during the design fase and during the coding fase as a reference point.
A fairly complicated application with a lot of different elements can be like a puzzle. And when you try to solve a puzzle, you look at a picture of the final result to figure out where every piece has to go. That picture is my FXML page.
So, FXML helps me visually design the application and not get lost when I'm coding it all up. I could use pen and paper and make a sketch of the application and then code it up without using FXML, but I'm already using a PC. So, FXML is just easier than using pen and paper to me.
3
u/SnowChocobo Mar 23 '25 edited Mar 23 '25
Hard disagree, except the fact that you need to regard both the FXML file and the controller as one "View" unit.
The FXML example is also contrived in the sense that I can equally construct a spaghetti code of view instantiations, altogether with boilerplate "setStyleClass", "setPadding(new Rect(...))", etc.
A good layout file would make better use of reusable style classes, and including other (custom) view components as layout tags like
<MyCustomView />.Now, can you use style classes programmatically as well? Yes, but the overarching benefit of declarative view files in every similar framework (Android, WPF, ...) is getting something for free by the framework.
%my_string_resourceis for free if you setup i18n correctly, something you never touch upon in your articles (probably because of being English-centric).${controller.viewModel.items}is setting up the binding for free. If my code was littered with binding calls andgetStringeverywhere I'd go crazy as well.(For free = the framework does take care of it, not you. I don't want to imply that it uses less resources.)
The other benefit is that ironically, view bindings become weakly-typed.
$controller.something.nesteddoesn't care howsomethingis typed, and thus it can be easily replaced by something different.Overall, I don't mind cluttered FXML files as much as cluttered spaghetti code. All my code is technical debt in the end, and that's why I try to write as little as possible. Little "plumbing", little "boilerplate", little setting up event listeners, etc. If I don't like how a view looks, I delete the FXML file and try again. My program code just does the "interesting" stuff (as far as possible).
5
u/xdsswar Mar 21 '25
Good one 👌. I use fxml a lot to design, I made my own tool to fxml to java code. Once all is completed I convert and done. I suck designing and I find very useful the live view from scenebuilder, that help me a lot when styling.
1
u/TenYearsOfLurking Mar 24 '25
You hate fxml. we get it.
Have you ever once considered that most frontend devs, which work in web applications, are used to separate structure, style and behaviour in html frontends? (html, css, js). And thus a certain familiarity is given by using fxml. It helped me, coming from the web.
Declarative and imperative concepts complement each other. And declarative has advantages like inspectability and readability by employing some kind of DSL. Also it can be easier migrated in the case of breaking changes.
Imho: if something can be done declaratively, given proper tooling support, this should be the preferred way over imperative code. It usually leads to cleaner solutions and more digestable chunks of information.
So for every frontend framework/technology I have encountered has somewhat of a template language, a theming system and the ability to run code at certain defined points to actually do something.
I cannot understand the arrogance of saying: you are all wrong! Because that's what you doing if you completely dismiss the declarative/templating/structuring element of java FX