12

Oracle docs aren't very clear on this, google and bing neither.

Can I (like in WPF) bind to a model in FXML skipping the binding code in Java?

I did see, for a brief flash, a syntax like ${object.field} on a blog somewhere but I can't be sure.


LATER EDIT : I have been doing XAML development (WPF, Silverlight, Windows Phone) for quite a few years and I've been accustomed to expressing databindings in the markup. Furthermore I've read in a 2011 article on FXExperience that

I could turn things around a little bit and move the binding into the FXML document. This would allow tools to handle data binding in addition to layout. Note that the following code does not work today because bidirectional bindings are not supported in FXML, but we are working on fixing that.

Practically it was like :

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import fxmlapp.Model ?>

<VBox fx:id="root" xmlns:fx="http://javafx.com/fxml" fx:controller="fxmlapp.Sample">
    <fx:define>
        <Model fx:id="model" />
    </fx:define>
    <children>
        <TextField fx:id="firstNameField" text="${model.person.firstName}" />
        <Label fx:id="messageLabel" text="${model.person.firstName}" />
    </children>
</VBox>

Since it was in a 2011 article things could have been implemented in the meanwhile, but have not found any evidence in this direction. Therefore, after careful searching, I decided to ask here.

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
  • can you define in briefy exactly what you tring to achieve ? – Anshul Parashar Nov 13 '13 at 05:23
  • I hope the 'LATER EDIT' clarifies things a bit, now. – Andrei Rînea Nov 13 '13 at 09:15
  • I still don't get it. FXML databinding is in many cases very similar to XAML (but not as mighty, for example, no 'complex' expression binding) and the example you posted just works like that. What do you want to know? Do you want to see a simple sample that shows databinding in FXML? – zhujik Nov 13 '13 at 13:08
  • I am not sure the above FXML is valid and it works as expected today (JavaFX 2.2). The article in 2011 states that at that time it wasn't. – Andrei Rînea Nov 13 '13 at 13:15
  • It seems there is no bidirectional binding available in FXML. Just simple binding (from the model to the view/FXML) – Andrei Rînea Nov 13 '13 at 13:27
  • .. in other words changing the text in the TextField (manually, at runtime) will not push the new value in the model. – Andrei Rînea Nov 13 '13 at 13:36
  • 6
    No, bidirectional binding is not supported in FXML yet. You can bind bidirectional in the code, however. – zhujik Nov 13 '13 at 14:52
  • 2
    in code I know how to do it and it works. Well,thanks, this is a valid answer. Why don't you write it as an answer so I can accept it? – Andrei Rînea Nov 13 '13 at 18:03
  • This would be great to have because binding can be good for GUI related things, like enabling/disabling buttons if data is missing, etc. –  Sep 10 '15 at 02:34

1 Answers1

17

I'm digging up this thread because A) it doesnt have a resolution and B) I'm a shameless point theif.

edit: speaking to the original question, your post is exactly right, to do a (one way) binding, the syntax is ${fx_id_value}, exactly as written in the OP's example.

It seems to me that in FXML 2.1 there was some notion of bidirectional bindings added, as per javafx.fxml.FXMLLoader#BI_DIRECTIONAL_BINDING_PREFIX

/**
 * Prefix for bidirectional-binding expression resolution
 * @since JavaFX 2.1
 */
public static final String BI_DIRECTIONAL_BINDING_PREFIX = "#{";

unfortunately, following that back a little bit, we effectively only have one usage on line 318 of javafx.fxml.FXMLLoader.Element#processPropertyAttribute:

else if (isBidirectionalBindingExpression(value)) {
  throw constructLoadException(new UnsupportedOperationException("This feature is not currently enabled."));
}

Doing a search on the javafx bug tracker reveals that this exposed javadoc, referencing #{ as a symbol, is a bug, and should be removed. This seems to me to be something that is in the works. However, considering the myriad of bugs JavaFX currently has, I wouldn't be surprised if we don't see it until java 9.

Stepping back, your frustration with the documentation mirrors my own (as does your experience: I'm also coming at JavaFX from WPF, granted I've spent much less time with both than I suspect you have). The best solution for using javafx binding expressions has come from reading the source code to the com.sun.javafx.fxml.expression.Expression (warning! usual rules about com.sun packages apply: they are internal and they have wierd licensing issues, read that source at your own risk!). The Expression.java class is a hand-written parser/compiler for javafx binding expressions withen FXML, using reflection as its runtime. Why they didn't use ANTLR or JavaCC I don't know. It would be much easier to read if we could just reference a grammar and a listener.

From reading that, I've learned that you can have expressions withen the ${ and } delimeters. The most common for me has been something like:

<HBox>
  <CheckBox fx:id="abcCheckBox"/>
  <TextField disable="${ ! abcCheckBox.selected}"/>
</HBox>

similar to XAML. It does also support addition, negation, and other common things, as long as everything references fx:id'd components and properties on them (and 'controller' is an fx:id given to you for free referencing the controller this XML is being loaded onto).

All things considered, I like javafx, but it will be a while before they have the deep binding & integration features that something like C#, WPF, XAML, and caliburn-micro offer.

Groostav
  • 3,170
  • 1
  • 23
  • 27
  • 1
    We are in Java 14 now and this still isnt fixed. – Andez Sep 10 '20 at 11:50
  • 1
    Yeah @Andez it looks like JavaFX is going to be a reasonably nice but under-developed UI framework. Many of the critical problems with FXML have been solved, but its usability is still very low compared to XAML. At this point I'm thinking most of my UI will be done in tornadofx.io, which is nice but WYSISYG editors (requires FXML) are really nice for our process (read: to discuss and mock things) – Groostav Oct 13 '20 at 19:49