r/JavaFX • • Aug 26 '24

Discussion Do you use FXML?

3 Upvotes

18 comments sorted by

View all comments

Show parent comment

2

u/SpittingBull Sep 05 '24

Now we're there. You have no current knowledge of SceneBuilder (and maybe FXML) but still suggest the OP should forget about it - without any real explanation.

That helps nobody.

It is great that you found a solid solution for yourself. But that does not negate the fact that FXML is a solid concept that can help to reduce development cycles and maintainability in many cases.

Back to some facts:

You claim that I need to write 12 lines to create a label and a binding. I say I need 2.

@FXML Label label; : label.bind(...);

I don't have to edit FXML since SceneBuilder takes care of that.

In regards of the presentation model I use both logic classes that are bound to every controller on one side and the data model on the other side. In addition I use a couple of custom FXML properties for additional state control.

Data models are implemented as sets of JavaFX properties and their getters/setters. These properties can easily be bound to control values.

I have extended pretty much every JavaFX control class. They all keep their original value (the value that came from the data model), their initial value (the accepted value of the last save event) and a change indicator in the form of a boolean property.

The controller has a global change property which is bound to each of its childrens change properties . The binding is established at the end of the controllers initialization by walking through it's child node tree.

Let me illustrate with an example:

Let's say I want a save button in a toolbar which should only be enabled when there actually is something to save.

This is simply done by binding it's disabledProperty to the controllers change property.

Now let's say the user enters text in an TextField. My TextField has a subscription for the textProperty. Each change will be compared with the initial value and the change property will be adjusted accordingly.

Since the controllers change property is bound to the TextFields one, the save button will be toggled accordingly.

Another one:

Let's say a cell in a TableView needs to have a different background if the cell content has a certain value.

Now my TableViews cell factory will invoke a custom function for every cell if this function is not null.

The logic class for the TableViews controller on the other hand sets this very function.

The color change will be done by setting a PseudoClass that is defined in the applications CSS file.

So what I am saying is that there are ways to implement a clean presentation model while utilizing FXML.

Property bindings and subscriptions are the key.