July 2, 2016
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.
Select Kotlin from the list of project types, select Kotlin (JVM), and press the Next Button.
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.
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.
Press Ok.
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.
Press Ok to dismiss the Project Structure window. Expand the newly-created project.
Right-click on the src directory and select New > Kotlin File/Class from the main menu.
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.
Select Run > Edit Configurations from the main menu.
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.
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.
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.
Visit the TornadoFX page on GitHub for more examples and the source code.
By Carl Walker
President and Principal Consultant of Bekwam, Inc