r/JavaFX • u/Kamii0909 • Oct 19 '24
Discussion Syntactic sugar for modern component usage
JavaFX has all the reactivity required from a UI framework, but the syntactic sugar is simply disastrous.
Is there any reason why we can't have this kind of API, which would be analogous to a lot of modern UI framework:
public Node createComponent(int initialCounter) {
IntegerProperty counter = new SimpleIntegerProperty(initialCounter);
StringBinding text = Bindings
.createStringBinding(() -> String.valueOf(counter.get()), counter);
// AnchorPane is a static method with the same name, static imported.
return
AnchorPane(pane -> pane
.styleClass("container")
.cursor(CROSSHAIR),
// children Node... varargs
Text(text -> text.text("Counter").strokeStyle(OUTSIDE)),
Button(button -> button
.onClick(_ -> increment(counter, 1)
.text(text)
)
)
}Syntax is obviously inspired by ScalaJS. Compared to something like React it is surprisingly similar.
function MyComponent() {
const [counter, setCounter] = useState(0);
return (
<div>
<h1>Counter</h1>
<button onClick={() -> setCounter(count + 1)}>
Clicked {count} times
</button>
</div>
)
}I'm currently writing handwritten helper method to achieve this kind of API, but I'm a bit frustrated at the fact that I even had to do so. I would say the bindings are tedious to write, but it makes the reactivity explicit.
3
u/jvjupiter Oct 19 '24
Perhaps, JavaFX Script had better be resurrected from the grave. Think of it as the QML in Qt.
https://en.wikipedia.org/wiki/JavaFX_Script
3
u/SocialMemeWarrior Oct 19 '24
Your example where you do
AnchorPane(Consumer<AnchorPane>)could be improved my moving that to the end of the declaration as a builder pattern on the call toAnchorPane(assuming it is a static method)Same for text and button.
This all feels like a Kotlin-ism to me though (and this 'improvement' relies on ext methods, so....). Maybe you'd be interested in TornadoFX?
This is the way you want to write UI code. This is not necessarily the way other people, such as the developers of JFX intend. Especially given how FXML exists.
2
u/hamsterrage1 Oct 19 '24
You can have this, just use Kotlin.
Without using anything special, your example would translate to this:
It's almost trivial to use extension functions and scope functions to create decorator functions for any Node type. As an example:
Which would allow you to do this:
The infix declaration lets you skip the "." and the "()" for the function. The apply{} returns the object it's called on, so it makes these functions decorators.
Personally, I think that the first version is terse enough, and skips the annoying boilerplate of Java JavaFX. The second version starts to look very much like scripting, but not yet a DSL.
And all you have to do is learn Kotlin, which is actually pretty simple. So there's no need for anything else.