A New JavaFX App Framework for Kotlin — TornadoFX
How to use the TornadoFX MVC framework with JetBrains' Kotlin JVM language.
Join the DZone community and get the full member experience.
Join For FreeTornadoFX is a JavaFX application framework for Kotlin, the new statically typed JVM language from JetBrains.
Why Yet Another Framework?
Kotlin interoperates perfectly with Java, so writing JavaFX apps with Kotlin is already a pleasure. However, TornadoFX makes use of some very interesting language features of Kotlin that enables even more concise and beautiful code without hiding any features of the JavaFX API.
Quick Overview
TornadoFX is an MVC framework. A View contains the root node of the UI as well as the view logic. Controllers contain the business logic. Elegant async support ensures that long running tasks run in a background thread and that the result is applied on the UI thread.
It comes with a REST client and built in JSON support. Other features include dependency injection, automatic FXML view loading, type safe builders, and various enhancements to standard JavaFX API's made possible with Kotlin's extension functions.
What It's Not
TornadoFX does not have any visual components. There is no docking framework, no special toolbar or action interfaces for you to implement like what you would find in RCP frameworks like Eclipse RCP and NetBeans RCP. While such frameworks are great for some types of applications, they often make you carry baggage you don't need. They also add to the learning curve.
You can learn TornadoFX in an afternoon and be instantly productive because you are using your existing JavaFX knowledge on top of a very light framework that doesn't force you into a lot of new patterns. The principal design goal is to provide just what you need, without getting in the way.
What Does it Look Like?
Here are some code samples to give you a feel for the framework.
Hello World View
class HelloWorld : View() {
override val root = HBox(Label("Hello world"))
}
View Loaded From HelloWorld.fxml With Additional init Operations
class HelloWorld : View() {
override val root: HBox by fxml()
val label: Label by fxid()
init {
label.text = "Hello world"
}
}
A Controller That Makes a REST Call and Converts the JSON Result to a List of Customer Objects
class CustomerController : Controller() {
val api : Rest by inject()
fun listCustomers(): ObservableList<Customer> =
api.get("path/to/customers").list().toModel()
}
Add a Row to a GridPane
grid.row {
label("Name") {
addClass("fieldLabel")
}
textfield {
promptText = "Enter your name"
textProperty().bindBidirectional(user.name)
}
}
Add User Selection Listener to a TableView
contactsTable.onSelectionChange {
editContact(it)
}
Run a Background Job and Update the UI When Finished
background {
customerController.listCustomers()
} ui {
customerTable.items = it
}
Download
TornadoFX is available at Maven Central.
<dependency>
<groupId>no.tornado</groupId>
<artifactId>tornadofx</artifactId>
<version>1.4.1</version>
</dependency>
Take It For a Spin
You can clone the example application repository on GitHub to get a basic skeleton up and running quickly.
Head over to the Wiki for complete Documentation and more usage examples. I'd love to get your feedback!
Enjoy JavaFX and Kotlin. :)
Opinions expressed by DZone contributors are their own.
Comments