-
Notifications
You must be signed in to change notification settings - Fork 271
Type Safe Builders
Wiki ▸ Documentation ▸ Type Safe Builders
JavaFX is a bit verbose, as is Java. Kotlin's apply
and with
functions, in combination with the shortened accessor syntax and the lack of a new
operator and other tricks goes a long way to make your code more readable.
There is nothing wrong with sticking to pure Java FX and the Kotlin syntax. However, if you would like to cut down even further on the boiler plate, and describe your views in a declarative and consice way, you could take a look at the type safe builders of Tornado FX.
The builders contain no magic. In fact, they are very simple extension functions to the Java FX Pane
class, which enables you to create a new node, set some properties and add it to the children
list of the parent Pane
with very little code. The hierarchical nature of the builders makes it extremely easy to understand the ui composition with a simple glance.
hbox {
label("Hello world") {
addClass("heading")
}
textfield {
promptText = "Enter your name"
}
// You can also add nodes that are not created with the builders
this += Label("Plain old label")
}
The full list of available functions can be seen in the Builders.kt source file. If you miss a feature, send us a pull request, and we'll be happy to include it.
Next: Async Task Execution