Skip to main content

TornadoFX Overview

TornadoFX is a lightweight framework for building JavaFX applications using the Kotlin programming language.

It provides a simple and concise syntax that allows developers to create elegant and highly maintainable user interfaces.

Introduction

TornadoFX is designed to simplify the process of building JavaFX applications by leveraging the power and expressiveness of the Kotlin language. It provides a number of features and conventions that make it easy to create responsive and visually appealing user interfaces.

History

TornadoFX was created by Edvin Syse in 2015 as a way to bring the benefits of the Kotlin language to JavaFX development. Since then, it has gained popularity among Kotlin developers for its simplicity and ease of use.

Features

Declarative UI

TornadoFX allows you to define your user interface using a declarative syntax. This means that you can express the structure and layout of your UI in a concise and intuitive way. Here is an example of a simple UI defined using TornadoFX:

class MyView : View() {
override val root = vbox {
label("Hello, TornadoFX!")
button("Click Me")
}
}

In this example, we define a MyView class that extends the View class provided by TornadoFX. The root property is where we define the structure of our UI using a DSL provided by TornadoFX. In this case, we use the vbox function to create a vertical box layout, and we add a label and a button to it.

Type-Safe Builders

TornadoFX provides type-safe builders that allow you to create and configure UI components in a concise and type-safe manner. This helps to catch common errors at compile-time and improves the readability of your code. Here is an example of using a type-safe builder to create a button:

button("Click Me") {
action {
println("Button clicked!")
}
}

In this example, we create a button with the label "Click Me" and attach an action to it. When the button is clicked, the action will be executed, and the message "Button clicked!" will be printed to the console.

Dependency Injection

TornadoFX provides a built-in dependency injection mechanism that allows you to easily manage dependencies between components. You can inject dependencies into your views, controllers, and other components using the DI class provided by TornadoFX. Here is an example of injecting a dependency into a view:

class MyView : View() {
val myService: MyService by inject()

override val root = vbox {
button("Click Me") {
action {
myService.doSomething()
}
}
}
}

In this example, we inject a MyService dependency into the MyView class using the inject() function provided by TornadoFX. We can then use the injected dependency to perform some action when the button is clicked.

Event Handling

TornadoFX provides a convenient event handling mechanism that allows you to handle user interactions and other events in a declarative and type-safe manner. You can bind event handlers to UI components using the onEvent function provided by TornadoFX. Here is an example of handling a button click event:

button("Click Me") {
action {
println("Button clicked!")
}
}

In this example, we define an action to be executed when the button is clicked. When the button is clicked, the message "Button clicked!" will be printed to the console.

Internationalization

TornadoFX provides support for internationalization out of the box. You can easily define localized strings and use them in your UI using the messages property provided by TornadoFX. Here is an example of using localized strings in a label:

label(messages["hello"])

In this example, we use the messages property to retrieve a localized string with the key "hello" and set it as the text of the label.

Validation

TornadoFX provides a powerful validation framework that allows you to easily validate user input and provide feedback to the user. You can define validation rules for your UI components using the validator property provided by TornadoFX. Here is an example of validating a text field:

textfield {
validator {
if (text.isBlank()) {
error("Field is required")
}
}
}

In this example, we define a validation rule for a text field. If the text field is empty, an error message with the text "Field is required" will be shown.

Examples

Here are a few examples demonstrating the usage of TornadoFX:

  • Hello World: An example that shows how to create a simple "Hello, World!" application using TornadoFX.
  • TodoFX: An example that shows how to create a todo list application using TornadoFX.
  • Calculator: An example that shows how to create a calculator application using TornadoFX.

Conclusion

TornadoFX is a powerful and flexible framework for building JavaFX applications using the Kotlin programming language. It provides a number of features and conventions that make it easy to create responsive and visually appealing user interfaces. With its declarative syntax, type-safe builders, dependency injection, event handling, internationalization, and validation support, TornadoFX simplifies the process of building complex UIs and helps developers write clean and maintainable code.

For more information, you can visit the official TornadoFX website: https://tornadofx.io/