Skip to content

Resources

Edvin Syse edited this page Jan 26, 2017 · 3 revisions

WikiDocumentationResources

Easy access to application resources

Lot's of JavaFX APIs takes resources as an URL or the toExternalForm of an URL. To retrieve a resource url one would typically write something like:

val myAudioClip = AudioClip(MyView::class.java.getResource("mysound.wav").toExternalForm())

Every Component has a resources object which can retrieve the external form url of a resource like this:

val myAudiClip = AudioClip(resources["mysound.wav"])

If you need an actual URL it can be retrieved like this:

val myResourceURL = resources.url("mysound.wav")

The resources helper also has several other helpful functions to help you turn files relative to the Component into an object of the type you need:

val myJsonObject = resources.json("myobject.json")
val myJsonArray = resources.jsonArray("myarray.json")
val myStream = resources.stream("somefile")

It's worth mentioning that the json and jsonArray functions are also available on InputStream objects.

Resources are relative to the Component but you can also retrieve a resource by it's full path, starting with a /.

Next: Properties