Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top