r/JavaFX • u/mstr_2 • Aug 23 '26
I made this! FXML/2 for JavaFX
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:
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.
Elements named with
fx:idcan be directly referenced in the code-behind Java class (no@FXMLinjection required).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.
All binding modes that JavaFX offers are supported:
one-time:
$foounidirectional:
${foo}bidirectional:
#{foo}reverse:
>{foo}content forms:
$..foo,${..foo}, etc.
Custom markup extensions. For example, you could define an
I18nmarkup 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.
2
u/alex_tracer Aug 26 '26
Are there any example project to demonstrate advantages?
1
u/mstr_2 Aug 26 '26
I'm using FXML/2 in a large, proprietary application (which was the reason why I created FXML/2 in the first place, because classic FXML just isn't good enough). I recommend reading the MVVM tutorial; it explains quite a lot of advanced topics.
4
2
u/Fuzzy-System8568 Aug 27 '26
Were LLM agents used in the making of this project (not accusing, just asking)
1
u/Guilty_Village_958 Sep 14 '26
why not make a dsl in java/kotlin instead of another markup language if this is not intended to be compatible with scene builder? u get all type safety. https://github.com/luvml/luvjfx ; sample below (it is intended to replicate/mirror html/fxml dom heirarchy of attributes and nodes in java code) :
import static luvjfx.Fx.*;
var name = textField().promptText("Your name");
var form = vbox().styleClass("form").spacing(8).padding(16).nodes(
label("Booking").style("-fx-font-size: 18; -fx-font-weight: bold;"),
label("Name"),
name,
button("Book").defaultButton(true)
);
var scene = scene(borderPane().center(form), 440, 380);2
u/junkri Sep 14 '26
I do the same with scalafx, but sometimes these libraries lag jfx version updates etc
1
u/Guilty_Village_958 Sep 14 '26
i have plenty of escape hatches ; but that apart i still feel FXML has it's own value, u can see components and visually edit, nowadays i am not hand coding at all, it is all ai, and to tell ai what to make by words it very cumbersome compared to just making it in scenebuidler
3
u/emberko Aug 24 '26
Do you have any plans to contribute this work to the upstream, e.g., as an incubator module? As far as I recall, this isn't the first FXML compiler implementation. The first one required the entire Micronaut core in the classpath, and the second relied on a custom DI runtime. Yours is much cleaner, no external dependencies and uses (superior) MVVM over MVC. So this is essentially XAML for JavaFX, and official support would be a huge boost.
Could you also clarify the intended workflow for FXML/2? Is it meant to be written manually, converted back and forth with FXML/1, or do you plan to support existing tools like Scene Builder, or maybe build your own?
Anyway, thank you for this work! This is something that has been awaited for a long time.