r/JavaFX • • Feb 26 '25

Discussion Why do some developers vouch for creating even the base UI with code?

As We also know we have fxml and Scene Builder to properly set up the initial design. So why not use that?

The only problem that i've read is that it is slightly slower. I know we may need code to create some dynamic nodes. But the initial layouts of nodes that needs to be added dynamically can be created in fxml and then modified based on our requirements. Eg:

I have this ActivityContainer built in scenebuilder(//to show activities in my small hotel app)

And that ActivityContainer will filled up by the Controller class after the admin of the hotel fills up the required details in:

Then i will add the ActivityContainer in the main page.

Benefit of using fxml:
You can style easily and position the nodes the way you want.(and don't need to run the code zillion times to see if you everything is looking ok)

10 Upvotes

20 comments sorted by

View all comments

Show parent comments

1

u/AdeptMongoose4719 Feb 27 '25

u/hamsterrage1 Ok i understand that fxmls are slower. But can you tell some scenarios where you found it cumbersome?

1

u/KapFlagon Feb 27 '25

hamsterrage1 has previously posted an insightful article that really dives into their opinions on FXML. If you want the full details, give it a read.

1

u/hamsterrage1 Feb 27 '25

Anything that you need to do programmatically to any screen element that you define in FXML has to be done via an FXML Controller. 

This includes any access to anything not static in your layout. You can apparently define one-way bindings from a presentation model, but I rarely see this done...because it's freaking complicated!!!

But it won't help you with outward, or bidirectional bindings.  So any update functions are going to have to have bindings declared in the FXML Controller...which means declaring the Nodes as fields in the FXML Controller, with the FXML annotation. 

If you are using a framework and defining a presentation model, then how do you get it into the FXML Controller????  Since the FXML Controller is instantiated by the FxmlLoader, you'd need to use a ControllerFactory in order to have that presentation model passed as a constructor parameter so that it is available when the initialize() method is run. Once again, this is freaking complicated!!!

Beginners don't do this stuff because the whole FxmlLoader process is mysterious and magical and utterly beyond their comprehension.  It's a nightmare for beginners, so virtually everything they do is already going off the rails right at the beginning.  

Almost every time I see AnchorPane used in a layout, I know that it was created from SceneBuilder.  AnchorPane is the appropriate solution for about 1% of the JavaFX use cases, so the fact that that you see it in 99% of the SceneBuilder created layouts is a definite smell of something.  

My approach is to apply DRY and push everything that occurs repeatedly across all my projects in builder methods.  As an example, I'll often have Labels styled a particular way and bound to some StringProperty. So I'll create a builder method with a name for that style and just pass it the StringProperty. Something like promptOf(text: ObservableStringValue).

The end result is that my layout code is just layout, and the data bindings are baked into the builders. It's way easier to write and read than FXML. 

And as that goes, I can write layout code while building the presentation model and supporting it with business logic in the appropriate place to supply sample data faster than you probably can create a layout in SceneBuilder that still has no connection to a presentation model or a framework.  

FXML is the price you pay for getting to use SceneBuilder.  But SceneBuilder isn't worth that price.  Not by a long shot.