Buğra Ekuklu
Buğra Ekuklu

Reputation: 3278

Prefix path for FXML files in TornadoFX

Is there a way to provide a path for FXML files, used by TornadoFX, with using its convention by fxml()?

Normally, TornadoFX conventionally tries to locate FXML resources in src/main/resources, however, our application is large and this might now be the best idea. In our case, we would like to maintain the files in a subdirectory, i.e. src/main/resources/fxml.

We would like to set it up during application startup. Is this possible?

Upvotes: 0

Views: 226

Answers (1)

Edvin Syse
Edvin Syse

Reputation: 7297

I've added an FXML locator function to the framework so that you can override this to change the resource location. The declaration and default implementation looks like this:

var fxmlLocator: (component: UIComponent, location: String?) -> URL = { component, location ->
    val targetLocation = location ?: component.javaClass.simpleName + ".fxml"
    requireNotNull(component.resources.url(targetLocation)) { "FXML not found for ${component.javaClass} in $targetLocation" }
}

You can override this in app.init() for example, like this:

FX.fxmlLocator = { component, _ ->
    val targetLocation = "/fxml/${component.javaClass.simpleName}.fxml"
    requireNotNull(component.resources.url(targetLocation)) { "FXML not found for ${component.javaClass} in $targetLocation" }
}

However, if you go this route you must pay attention to your class names, as the same class name in different packages would look for the same resource in /fxml. Alternatively, change the implementation to observe the package names as well.

I'm committing the feature now, you can try it out tomorrow using the latest snapshot release from sonatype.

Upvotes: 1

Related Questions