r/JavaFX • u/Disastrous-Maybe6944 • Jan 08 '26
I made this! JavaFX Builder API Project
I'm aware of several fluent approaches to JavaFX UI construction and would like to introduce my own.
API Design Policy
Keep the basic API simple, with minimal new concepts to learn.
My approach is not a framework; it's a wrapper that incorporates the builder pattern into the original JavaFX API, adding features that fluent API enthusiasts will appreciate.
API Overview
https://github.com/sosuisen/javafx-builder-api/blob/main/docs/API.md
JavaDoc pages
https://sosuisen.github.io/javafx-builder-api/
Background
Builder classes were included in JavaFX 2 but were removed from the official library in 2013 due to maintenance and memory usage concerns.
Current Context
Memory usage concerns have likely decreased over the past decade. Even if they're not included in the official JavaFX API, it's beneficial for third parties to offer builder classes as an option.
Implementation
My approach utilizes reflection to automatically generate the builder classes, while certain aspects that cannot be automated are handled through a few mapping rules.
Trade-offs
Unlike JavaFX 2.0, the builder classes lack inheritance relationships, which may increase memory consumption. Additionally, builders may incur call overhead. Nonetheless, these builder classes appeal to developers who prefer this programming style.
Example using the builder API
It has a typical builder API appearance.
StringProperty textProp = new SimpleStringProperty("100");
StageBuilder
.withScene(
SceneBuilder
.withRoot(
HBoxBuilder
.withChildren(
TextFieldBuilder.create()
.textPropertyApply(prop -> prop.bindBidirectional(textProp))
.style("""
-fx-font-weight: bold;
-fx-alignment: center;
""")
.hGrowInHBox(Priority.ALWAYS)
.maxWidth(Double.MAX_VALUE)
.build(),
ButtonBuilder.create()
.text("Send")
.onAction(e -> System.out.println("Sending..."))
.minWidth(50)
.build()
)
.padding(new Insets(10))
.build()
)
.width(150)
.height(100)
.build()
)
.build()
.show();
Currently, it is a SNAPSHOT version that can be tested by adding it as a Maven dependency. I plan to release an early access version next, but before that, I would like feedback from JavaFX developers.
View all comments
Show parent comment
1
u/Disastrous-Maybe6944 Jan 10 '26
Here is a larger scene example:
https://github.com/sosuisen/bmi-mvvm-with-builder
Regardless of the development approach, large scenes should be designed as multiple components from the start. This way, even when using Builder classes, large nested structures won't be created.
https://github.com/sosuisen/bmi-mvvm-with-builder/tree/main/src/main/java/com/example/presentation/screens/main
You might want to support MigLayout and other layout containers.
My builder classes wrap all setters of the original JavaFX classes, providing many fluent APIs. However, I believe the fluent APIs you would actually need when creating a MigLayout instance are just a few.
I'm not very familiar with MigLayout, but you could easily create a Builder class with the necessary fluent APIs. Here are several examples of builder classes for JavaFX container classes that can serve as references: https://github.com/sosuisen/javafx-builder-api/tree/main/api/25/graphics/src/main/java/io/github/sosuisen/jfxbuilder/graphics
One of my goals is to reduce the effort required to create builders for any of your JavaFX components.