DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. A New JavaFX App Framework for Kotlin — TornadoFX

A New JavaFX App Framework for Kotlin — TornadoFX

How to use the TornadoFX MVC framework with JetBrains' Kotlin JVM language.

Edvin Syse user avatar by
Edvin Syse
·
Jan. 28, 16 · Opinion
Like (9)
Save
Tweet
Share
29.57K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

TornadoFX 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. :)

Application framework JavaFX Kotlin (programming language) app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Comparing Kubernetes Gateway and Ingress APIs
  • 9 Ways You Can Improve Security Posture
  • The New DevOps Performance Clusters
  • How to Deploy Machine Learning Models on AWS Lambda Using Docker

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: