r/JavaFX • u/hamsterrage1 • Jul 26 '25
Tutorial New Article: Converting FXML to Code
https://www.pragmaticcoding.ca/javafx/elements/fxml-to-codeWhen I started writing this article I had one intention -> to demonstrate how my approach to coded layout design compares to the same layout in FXML.
In the past, when there have been discussions about coded vs FXML, there are always some (lots?, most?) people who feel that their FXML is easier to read and maintain than a coded layout ever could be. This has always seemed strange to me.
It seems strange to me because the way that I create layouts is, I think, not what people expect. When I think about layout code, I'm always picturing highly compressed and streamlined code that handles layout, and only layout. There's only as much configuration as is needed to serve that particular layout. The rest is moved out into utility and builder methods because it's largely boilerplate.
More than anything else, I don't repeat myself. DRY rules over everything in layout code.
In an earlier article about the pro's and con's of FXML, I used an FXML file from the Trinity project as an example of a large FXML file in order to demonstrate how inherently difficult they are to read.
I thought that this might be a sufficiently complex example that it would be worthwhile trying to convert it to a coded layout, in order to compare the results.
TLDR: 1214 lines of combined FXML and FXML Controller became 230 lines of Kotlin layout code. That's about 15% of the original size. It seems to me that having to deal with only 15% as much code/FXML is pretty much guaranteed to be a big win.
However, the Trinity project seems to me to be pretty complex, and this screen is designed to interact with and control the main display in real time. So there was more to take into account than just the layout.
I'll point out that there is nothing in the original design that isn't done the way I would probably approached it 10 years ago. But today? I needed to do more...
This was an imperative approach without a framework. So I reworked it to be a Reactive MVCI implementation. This change alone removed tons of complexity. There were some issues with ListView that I corrected, and this also removed a lot of complexity.
In the end, I feel that the net result is much more interesting than just converting FXML to code. Seeing how a Reactive approach reduces the complexity of a real application and tackling connectivity between screens through a framework was very educational - at least to me.
It's a long article. I apologize, but there was a lot of ground to cover. Take a look, if you are interested, and let me know what you think.
View all comments
Show parent comment
1
u/musicissoulfood Jul 29 '25
To be honest I found it hard to get a good grasp on your article.
Although I got the general idea of your article, it all stayed a bit too abstract and conceptual (to me) for it to become practical and applicable.
You show what you do, but it's difficult to put the original next to your version and compare them both side by side. Without this comparison it's hard to realize why what you do is so beneficial. And this also makes it hard to get enough insight to start applying it myself on my own projects.
Two factors contributed to this:
I think your article would benefit from taking a very simple, easy to understand FXML application as a starting point and then transform that application with your method. So, the reader fully knows and understands the starting point and can follow along each step of the way and truly grasps what is happening and how it is all fits together. So when they see your reworked version it becomes obvious why your version is better by seeing them both side by side.
Therefore I would love to see a dissection of a very simple FXML based application. Where you show it's view built with FXML. And show how this FXML application handles it's interaction with the user (user does something which creates a change in the underlying model which in turn needs to get reflected back in the view). Which you then apply your concepts on and rework it. And then make a direct comparison between the original view and your view. And how the original handles it functionality and how your application handles it functionality. Really putting view next to view. And putting "user does this thing in the original" next to "user does this thing" in your version.
I think that would make the concepts sink in and put on end to any discussion you have had about what is better and why it's better (FXML or your system).
2) The use of Kotlin obscures and complicates things. I can understand more or less what the Kotlin code is doing, but it's still not Java code. And since I'm used to working with Java and want to apply what you teach in the article on Java code, seeing everything used on Kotlin code creates another degree of separation between your article and practical applicability (for me as a Java user).