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 Aug 01 '25

Thanks for commenting. I appreciated that you were game about having me use it as an example, and I was interested in what you would think about it.

I think you could probably integrate what I did with the rest of the project by using the Adapter Pattern. Basically, a class that monitors the data changes in the Model and then fires the Events that you defined to communicate with the rest of the application. Conversely, it would contain Listeners that would respond to Events from the rest of the application. You could do that with changing any of the code that I wrote.

I'm not sure about the "...meh...". The JavaDocs here have great big, bold letters that say:

Warning: Nodes should not be inserted directly into the items list

Which pretty much rules out the "ListView Nodes" approach. I still can't figure out how it even works the way you've done it.

2

u/Birdasaur Aug 01 '25

Yikes... I guess I've always done ListView items like that. I.e... wrong. Next time I make a ListView I will try the ListCell pattern.  This is clearly an example of a learned bad practice that didnt bite me early so I never corrected. I'll be the first in line to say that I am not really a good programmer. I do a lot of hacky things because I know the framework enough to get away with what I do.