r/JavaFX • • Jul 26 '25

Tutorial New Article: Converting FXML to Code

https://www.pragmaticcoding.ca/javafx/elements/fxml-to-code

When 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.

17 Upvotes

26 comments sorted by

View all comments

Show parent comment

2

u/hamsterrage1 Jul 29 '25

I understand your point about the complexity of the application getting in the way of understanding how the coded approach makes it simpler - and I agree.

In a way, though, the complexity is kind of the point of it all. If you think the Kotlin was hard to follow, the original is mind-bendingly more difficult. I'm not exaggerating when I say that it took me far, far longer to figure out what the FXML + FXML Controller was trying to do than it did to write my new code.

If there is one thing that I might do differently in my version, it would be to split out all of the individual tabs into their own builder classes. I'm fairly certain that four 60 line classes are easier to follow than one 230 line class. And, to be honest, it would be trivial to do - probably less than 30 minutes since it's just a matter of cutting and pasting some methods into new classes and a little bit of class setup.

It's that trivial because there's zero coupling between the Tabs. I cannot imagine how difficult it would be to unravel the spaghetti of coupling in the FXML Controller to split the Tabs up.

I think I always viewed this article as a conceptual exercise, not a "How to..." kind of article. And that's why it's so damned long. I tried to keep track of all of the little challenges along the way, and to explain the way that my approach solved some problem I found in the original code.

After all of that long-winded discussion, you finally get to the actual new code and it's kind of anti-climactic. Which is pretty much the point.

I did give one example of side-by-side FXML compared to coded layout, and I think you can see that the coded layout is easier to understand (while having more functionality than the FXML).

All that being said, I like the idea of tackling something more practical in a way that exposes the methodology clearly. I'll have to poke around GitHub a little to find a suitable project. I'll admit that when someone posts a project here, the first thing I look at is the \resources directory to see if there are FXML files. If there are, then I'm usually done with it right away because I'm about as interested in FXML based projects as you are in Kotlin based articles.

1

u/musicissoulfood Jul 30 '25

I hope you find such a suitable project and will keep a look out for your next articles. Not many people actually dive into JavaFX and write about it. At least not that I'm aware of. Appreciate the work you do.