12. TornadoFX IDEA Plugin

To save time in using TornadoFX, you can install a convenient Intellij IDEA plugin to automatically generate project templates, Views, injections, and other TornadoFX features. Of course, you do not have to use this plugin which was done throughout this book. But it adds some convenience to build TornadoFX applications a little more quickly.

Installing the Plugin

In the Intellij IDEA workspace, press CONTROL + SHIFT + A and type "Plugins", then press ENTER. You will see a dialog to search and install plugins. Click the Browse Repositories button (Figure 13.1).

Figure 13.1 After bringing up the Plugins dialog, click Browse Repositories.

You will then see a list of 3rd party plugins available to install. Search for "TornadoFX", select it, and click the green Install button (Figure 13.2).

Figure 13.2 Search for "TornadoFX" and click Install

Wait for it to finish installing and the restart Intellij IDEA.

TornadoFX Project Templates

The TornadoFX plugins has some Maven and Gradle project templates to quickly create a configured TornadoFX application.

In Intellij IDEA, navigate to File -> New -> Project... (Figure 13.3).

Figure 13.3

You will then see a dialog to create a new TornadoFX project. You can create Gradle and Maven flavors, with or without OSGi support. Let's create a Gradle one for demonstration (Figure 13.4).

Figure 13.4

In the next dialog, give your project a name, a location folder, and a base package with your domain (Figure 13.5). Then click Finish.

Figure 13.5

You may be prompted to import the project as a Gradle project, and click on that prompt if you encounter it. You will then have a TornadoFX application configured and set up, including App, View, and Styles entities set up (Figure 13.6).

Figure 13.6

A generated TornadoFX project with a Gradle configuration.

These steps apply to the Maven and OSGi wizards as well, and do not forget to put your project on a version tracking system like GIT!.

Creating Views

You can create Views, Fragments, and FXML files quickly with the plugin. You can right click a folder in the Project, then navigate the popup menu to New -> TornadoFX View (Figure 13.7).

Figure 13.7

You will then come to a dialog to dictate how the View is constructed. You even have the option of specifying it as a Fragment instead through the Type parameter, as well as an FXML via Kind. Finally, you can specify the Node type for the Root, which should default to a BorderPane.

Figure 13.8

Click OK and a newView will generated and added to your project (Figure 13.9).

Figure 13.9 A new View generated with the TornadoFX plugin

Injecting Components

One last minor convenience. You can generate TornadoFX Component injections quickly with the plugin. For instance, if you right click the class body of the MainView, you can generate the MyOtherView as an injected property (Figure 13.10).

Figure 13.10

You can then use a dialog to select the MyOtherView as the injected property, then click OK (Figure 13.11).

Figure 13.11

Generating TornadoFX Properties

One of the most helpful features in the plugin is the ability to convert plain Kotlin properties into TornadoFX properties.

Say you have a simple domain class called Client.

class Client(id: Int, name: String) {
    val id: Int = id
    val name: String = name
}

If you click on a property and then the intent lightbulb, or press ALT+ENTER, you should see a menu popup with an option to convert it to a TornadoFX Property (Figure 13.12).

Figure 13.12

Do this for each property and your Client class should now look like this.

class Client(id: Int, name: String) {
    var id by property(id)
    fun idProperty() = getProperty(Client::id)

    var name by property(name)
    fun nameProperty() = getProperty(Client::name)
}

Your Client now uses JavaFX properties instead of plain properties. Notice the primary constructor will pass the intial values to the property() delegates, but you do not have to provide initial values if they are not desired.

This is a time-saving feature when creating domain types for data controls. Next we will cover how to generate TableView columns.

Generating Columns for a TableView

Another handy feature you can do with the plugin also is generating columns for a TableView. If you have a TableView<Person>, you can put the cursor on its declaration, press ALT + ENTER, and get a prompt to generate the columns (Figure 13.13).

Figure 13.13

You will then see a dialog to confirm which Person properties to generate the columns on (Figure 14.14).

Figure 13.14

Press "OK" and the columns will then be generated for you (Figure 13.15).

Figure 13.15

Note that at the time of writing this guide, for a given TableView<T>, this feature only works if the properties on T follow the JavaFX convention using the Property delgates.

Summary

The TornadoFX plugin has some time-saving conveniences that you are welcome to take advantage of. Of course, you do not have to use the plugin because it merely provides shortcuts and generates code. In time, there may be more features added to the plugin so be sure to follow the project on GitHub for future developments.

results matching ""

    No results matching ""