bekwam courses

TornadoFX HelloWorld in One File

July 2, 2016

Setup

Kotlin Icon

TornadoFX is a UI toolkit based on JavaFX for the Kotlin programming language. This article presents the simplest of TornadoFX programs using the IntelliJ IDE.

Select File > New > Project from the menu. The New Project Window is displayed. This screenshot shows the Ultimate version of IntelliJ, so you may have fewer project types available if you're using the Community version.

IntelliJ Screenshot Showing Kotlin Selection
IntelliJ New Project Window

Select Kotlin from the list of project types, select Kotlin (JVM), and press the Next Button.

IntelliJ Screenshot of Project Name and Location
Properties for New Project

Enter a project name and press the Finish Button.

Go to File > Project Structure in the main menu. Select the Libraries item on the left.

IntelliJ Screenshot of Default Project Libraries
Default Libraries of a Kotlin Project

Press the Plus Button and select From Maven. Enter “tornadofx” in the text field and press return. Select the most recent version of no.tornado:tornadofx. This is using 1.5.1 found on July 2, 2016.

IntelliJ Screenshot Showing Selection of TornadoFX from Maven
Specifying TornadoFX Library from Maven

Press Ok.

Choose Modules

Select the Project's sole Module and press Ok.  Note that I've called this project "tornadofx-test".  This does not point to an IntelliJ-defined test module.

IntelliJ Screenshot Showing TornadoFX Added
Project Libraries with TornadoFX Added

Press Ok to dismiss the Project Structure window. Expand the newly-created project.

IntelliJ Screenshot Showing Project Artifacts
Browsing Project Artifacts

Code

Right-click on the src directory and select New > Kotlin File/Class from the main menu.

IntelliJ Screenshot Showing a New Kotlin File Being Specified
Creating a New Kotlin File

A file HelloWorld.kt will appear in the src directory.

Paste the following code in the opened HelloWorld.kt file below the generated comment (/** */).


package demo;

import javafx.scene.control.Label
import javafx.scene.layout.HBox
import tornadofx.App
import tornadofx.View

class HelloWorld : View() {
  override val root = HBox(Label("Hello world!"))
}

class HelloWorldApp : App() {
  override val primaryView = HelloWorld::class
}

If IntelliJ isn't configured to automatically add imports, select Code > Optimize Imports from the main menu. The Kotlin file should be error free.

Run

Select Run > Edit Configurations from the main menu.

IntelliJ Screenshot of Empty Run/Debug Configurations
Run/Debug Configurations

Select the Plus Button and choose Application.

Enter a Name for the command (ex, “HelloWorldApp”) and navigate the the HelloWorldApp class using the Ellipse Button (...) next to the Main Class Text Field.

IntelliJ Screenshot of HelloWorldApp Run/Debug Configuration
HelloWorldApp Application Run/Debug Configuration

Press Ok.

Go to the Run > Run item on the main menu. When prompted, select the newly-created command HelloWorldApp. The console will be displayed in the IDE and the following tiny window will appear with the message from the Kotlin source.

Screenshot of JavaFX App Showing Label
Running the JavaFX Program

This article showed you how to run the simplest of TornadoFX programs using the IntelliJ IDE. After creating a Kotlin project, you added a dependency that pulled in the TornadoFX library. You then added a single Kotlin source file that contained TornadoFX code to display a JavaFX Label on a Stage. Finally, you created a command to execute the program.

Further Reading

Visit the TornadoFX page on GitHub for more examples and the source code.


Headshot of Carl Walker

By Carl Walker

President and Principal Consultant of Bekwam, Inc