ycomp
ycomp

Reputation: 8573

Launching a TornadoFX App from (an existing) Kotlin program

fun main(args: Array<String>) {

    HelloWorldApp().launch()
}

fun App.launch() {
    JFXPanel()
    Platform.runLater({
        start(Stage())
    })
}

This is what I do right now. Is there a better? more succinct way?

Is it safe to run multiple different TornadoFX apps from within the same kotlin program this way? I read something somewhere about a global variable so I'm wondering if only 1 is allowed/recommended.

Upvotes: 1

Views: 1659

Answers (2)

s1m0nw1
s1m0nw1

Reputation: 81899

You can start it like this:

fun main(args: Array<String>) {
    launch<HelloWorldApp>(args)
}

The launch function is defined in tornadofx package as a top-level function, HelloWorldApp is a random Application class.

Upvotes: 1

Edvin Syse
Edvin Syse

Reputation: 7297

The JVM already knows how to start JavaFX applications, and since your App class extends tornadofx.App which again extends javafx.application.Application, you can simply point your JVM to that main class and it will run just fine. If you want to explicitly launch your TornadoFX application, JavaFX provides a static launch method you should use.

A typical main function that starts a JavaFX or TornadoFX application would be:

fun main(args: Array<String>) {
    Application.launch(HelloWorldApp::class.java, *args)
}

JavaFX only allows the Application.launch function to be called one time in the life of a JVM, so you can't really start more than one anyways. However, TornadoFX provides special OSGi support allowing you to actually stop and relaunch other TornadoFX apps in the same JVM by utilizing an application proxy instance.

TornadoFX also supports JPro by using Scopes, which allows multiple application instance, though without actually calling Application.launch several times.

Upvotes: 4

Related Questions