Skip to content

gonozalviii/KopperFX

Repository files navigation

Release GitHub license

KopperFX

Kotlin Wrapper for JavaFX

About

KopperFX is a Kotlin Wrapper for JavaFX, which is supposed to simplify the use of JavaFX in Kotlin. For this purpose, extensions have been written especially for JavaFX. The goal is to be able to write JavaFX quickly and easily without unnecessary intermediate calls and without changing any core JavaFX concepts.

KopperFX was born in production use and extracted into a separate library as the same extension function were used over and over in different projects. Since then a lot of additional functions and functionality has been added.

Why not use TornadoFX?

When we started using Kotlin with JavaFX we of course took a look at the "lightweight" framework TornadoFX. And even though it contains a lot of interesting ideas there were also concepts that we absolutely did not like:

  • it forces you to learn a completely new language (DSL) to build your JavaFX application

  • it does not promote using FXML for building GUIs (though it does support it), which we think of as a killer feature especially when used with the excellent SceneBuilder

  • it contains functionality that we do not expect in a JavaFX wrapper (injection framework, REST client, additional controls, …​)

  • the resulting code from using the DSL is not very clear or intuitive from the technical perspective (why does this work?) and thus makes debugging harder

If you are completely new to JavaFX and want this complete package, TornadoFX might just be the thing for you.

We on the other hand want to address developers with experience in building JavaFX applications and do not want to relearn their entire skill set.

Our goal is to make your life just a little bit easier when developing JavaFX applications with Kotlin without forcing stuff onto you that you do not want or need.

Usage

To use KopperFX, it can be easily integrated as a dependency:

Gradle
repositories {
    ...
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.gonozalviii:KopperFX:0.6'
}
Gradle (Kotlin)
repositories {
    ...
    maven("https://jitpack.io" )
}

dependencies {
    implementation("com.github.gonozalviii:KopperFX:0.6")
}
Maven
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.gonozalviii</groupId>
    <artifactId>KopperFX</artifactId>
    <version>0.6</version>
</dependency>

Examples

The following examples show the differences between the current usage of JavaFX in Kotlin and the usage with KopperFX.

Handling JavaFX properties

import com.github.gonozalviii.kopperfx.extensions.*

val nameProperty = SimpleStringProperty("")
var name: String by nameProperty

Adding and removing Children

Kotlin
pane.children.add(node)
pane.children.addAll(node1, node2)
pane.children.remove(node)
pane.children.removeAll(node1, node2)
KopperFX
pane += node
pane.addAll(node1, node2)
pane -= node
pane.removeAll(node1, node2)

Adding and removing Items

Kotlin

tableview.items.add(item)
tableview.items.addAll(item1, item2)
tableview.items.remove(item)
tableview.items.removeAll(item1, item2)
KopperFX
tableview += item
tableview.addAll(item1, item2)
tableview -= item
tableview.removeAll(item1, item2)

Misc

Kotlin
//Selected Item
val item = tableView.selectionModel.selectedItem
tableView.selectionModel.select(item)

//Selected Index
val index = tableView.selectionModel.selectedIndex
tableView.selectionModel.select(index)

//Filechooser
filechooser.extensionFilters.add(FileChooser.ExtensionFilter("MP3", "*.mp3"))

//FX Thread
Platform.runLater { println("Kotlin") }

//AnimationTimer
object : AnimationTimer() {
        override fun handle(now: Long) {
                println("Kotlin")
        }
}.start()
KopperFX
//Selected Item
val item = tableView.selectedItem
tableview.selectedItem = item

//Selected Index
val index = tableView.selectedIndex
tableview.selectedIndex = index

//Filechooser
filechooser.addExtensionFilter("MP3", "*.mp3")

//FX Thread
fxThread { println("KopperFX") }

//AnimationTimer
animationTimer { println("KopperFX") }

Own ideas?

Please use the Github issue system :)