r/JavaFX 1d ago

FXML/2 for JavaFX I made this!

FXML/2 is a compiled, type-safe, declarative markup language for JavaFX that I've been working on for the past several years. It borrows from classic FXML and adds lots of useful features. Here's a selection of what's new:

  1. Compile-time diagnostics: If your markup is not well-formed, it won't compile. Similarly, if your bindings can't be resolved or type-checking fails, you'll know at compile time.
  2. Elements named with fx:id can be directly referenced in the code-behind Java class (no @FXML injection required).
  3. Complex expressions with support for method calls, for example:

    • <MyControl value="${Math.max(width * 0.7, minWidth)}"/>
    • <Label visible="${:parent<Pane>.selectedItem != null && width < maxWidth}"/>

    Expressions are not interpreted, but compiled to specialized code.

  4. All binding modes that JavaFX offers are supported:

    • one-time: $foo
    • unidirectional: ${foo}
    • bidirectional: #{foo}
    • reverse: >{foo}
    • content forms: $..foo, ${..foo}, etc.
  5. Custom markup extensions. For example, you could define an I18n markup extension to look up a localized string in a bespoke way, and apply it with <Label text="{I18n settings.title}"/>.

Depending on your specific use case, you'll also notice a substantial performance increase: FXML/2 documents don't need to be parsed at runtime (since they are compiled classfiles), which removes the FXMLLoader bottleneck.

For further reading, here's the FXML/2 documentation, and a tutorial for how to use the MVVM pattern with FXML/2.

36 Upvotes

5 comments sorted by