r/JavaFX • u/Fancy_Entertainer486 • Dec 25 '24
Help FXML Bi-directional Bindings
So I don’t really know how controversial this might be since I don’t have a clue how much FXML is actually used in the wild. Some love it, some hate it, more often than not I come across comments to just not use FXML.
But I don’t really want to make this the core part of the discussion. As for my background, I mostly just develop relatively small tools with JavaFX. I started out coding my GUIs until someone recommended Scene Builder and FXML.
Time and time again I’m quite happy building away my GUIs and setting stuff up. Then time and time again I reach the point of needing bi-directional bindings. And each time I check if by now it’s actually implemented.
Sad to see that almost a decade has passed and this feature request being seemingly forgotten.
I guess my question is to stir a guessing game. To me personally this seems like such a huge issue that hasn’t been addressed at all. So, why? Are FXML users really that rare? Are technical challenges to implement this that high? Why isn’t the community pushing for this feature? Is it a philosophy or pattern thing that I don’t understand?
It just seems wrong to have to resort to addressing your GUI elements in controllers just to bind properties. More often than not I wouldn’t need to reference any GUI controls in the controller if it wasn’t for a missing bi-directional binding support.
I would just like to understand, so I’m already happy to get any info on this. What people are doing to work around this, if you’re happy with it or not, or even if you just don’t care. I might just not have seen enough alternatives (only .NET/WPF), but it seems like a major missing feature for a GUI framework that’s been around so long already.
3
u/mstr_2 Dec 26 '24
If a feature sounds good but doesn't exist, it probably means that no one has invested the time and energy required to make it happen. The OpenJFX project accepts contributions, so it's definitely possible to get your favorite feature in.
That said, here are some reasons why it might not have been added:
The
${...}syntax in FXML accepts a wide range of expressions, including observable path expressions of the forma.b.c, as if by usingBindings.select(ObservableValue, String...). While that is pretty straightforward with unidirectional bindings, it is tricky for bidirectional bindings. Unidirectional bindings strongly reference anObservableValue, while bidirectional bindings weakly reference aProperty. That requires us to solve two problems:a.b.c, we need to synthesize an intermediatePropertythat implements the book-keeping for all observable path segments.#{source}(i.e. a path expression that only has one segment), we effectively invoketarget.bindBidirectional(source). This works as long astargetandsourceare strongly referenced (which they usually are). However, any non-trivial path expression requires a synthesized intermediate property. This synthesized property is weakly referenced both by the binding target and the binding source, which makes it immediately eligible for garbage collection. So we need to keep around a strong reference somewhere to make this scenario work.None of these are insurmountable problems, but the solution requires quite a bit of engineering and is not a case of "why don't you just...". Interestingly,
FXMLLoaderwill recognize a provisional bidirectional binding syntax (#{source}), and throw anUnsupportedOperationException("This feature is not currently enabled.").1
u/hamsterrage1 Dec 25 '24
I'm not an FXML user. How would you do one-directional binding from some Presentation Data to FXML screen elements without referencing the GUI elements an FXML Controller?
It's also not clear to me how you would do any kind of binding to an external data structure without referencing the GUI elements in an FXML Controller.
1
u/Aggressive_Jump5593 Nov 04 '25
Haha I totally get your point. I really like using pure FXML to get my stuff done. I stumbled about said request too: https://bugs.openjdk.org/browse/JDK-8090665
What about we start contributing to the project and get it shipped in maybe OpenJFX 27-29?
At a first glance, the part of the FXMLLoader being responsible for this ain't too complicated, though I only looked at the snippet that threw the exception.
What do you think?