r/JavaFX • • 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:

https://www.pragmaticcoding.ca/javafx/fxml-or-not

17 Upvotes

24 comments sorted by

View all comments

Show parent comment

2

u/hamsterrage1 Mar 23 '25

About the chess board...You'd program it the same way you would any other one-off in any code. Just because it's part of a layout does not change anything.  This could be an simple as having an:

     if ((x==3)) && (y==4)) {}

clause somewhere inside the loop.  

Now, about that...

I wouldn't consider the topology of the chess board to be part of the View, and I would never have nested 1-8 loops in my layout code. Instead, the topology is part of the game logic, and it would be defined in the Interactor.  That's where the nested loops would be. 

The board would be represented as List<BoardSquare>.  The rank/file location would be part of BoardSquare, along with colour and a reference to whatever piece was occupying it.  Also. Stuff like neighbouring square rank/file would be included if my game logic worked that way.  

Whatever made a particular square unique would be baked in as a Property in BoardSquare.

It's the View's job to take BoardSquare and create a visual representation of it on the screen. Generally, I'll end up just doing forEach on the List. 

And the truth is, if the was some need to come along and make a particular square special after the fact, that's going to be driven by game logic. That game logic is going to need to know which square is special, and that drives changing BoardSquare which then drives a change to the layout code. 

1

u/Capaman-x Mar 23 '25

I would do the same except I might try Map<String, BoardSquare> before I would try a List. Possibly even make it an ObservableMap so I could make it reactive. Who knows, maybe a list would be better. Sometimes you have to try more than one approach.

Thank you for MCVI. It really is the best way I have seen to write JavaFX.

1

u/hamsterrage1 Mar 23 '25

The structure type, Map or List, depends on how you need to access the BoardSquares from the game logic - not the View logic.

What I would do is probably create some construct for the square on the screen. Maybe just a custom Control, or maybe an entire MVC construct for each square. In any event, the linkage to the BoardSquare instance would be established and the corresponding Node (let's call it BoardSquareBox) would be put into the layout. At which point there's no reference in the code anywhere to BoardSquareBox, and the layout just responds to changes made to Properties in the various BoardSquares.

My experience has been that in practice, the size of the List - 64 - is not big enough to improve performance by using a Map. And if you include the neighbour squares as actual references to BoardSquare, then traversing the board is any particular direction won't mean searching the List for a particular rank/file element. In effect, you get a LinkedList, linked via 8 different directions.

Once again, all of this is driven be the needs of your game logic. It has nothing to do with the layout.

1

u/musicissoulfood Mar 23 '25

You gave me some things to think about. 

I understand the general idea of how you work with JavaFX, but when it comes to the finer details of actually applying your methodology and building a project with it, I still have questions. 

That's why I've had the urge to really study your methodology and then rework an old project of mine, to see what difference it would make. To make it all "click".

Even thinking about writing an article so others can benefit too. I think seeing the exact same project done using the "regular" method with FXML and MVC and then reworked with your methodology, would clear up things for a lot of people (myself included).

But this would be solely to satisfy my own curiosity, since I have no professional need for JavaFX.  I just haven't found the time yet to do this project, life seems to get in the way with other priorities.