drew moore
drew moore

Reputation: 32670

Referencing class resource in FXML

I have a JavaFX program that uses Maven, with a standard maven file tree:

enter image description here

How do I reference logo.png from within layout.fxml, assuming that layout is being inflated by a call to

Parent root = FXMLLoader.load(getClass().getResource("/fxml/layout.fxml"));

in MainApp.

I've trying to do so using

<image>
<Image url="@/graphics/logo.png" />
</image>

and have tried many variations on that file path (with and without the @), but keep throwing a

java.lang.reflect.InvocationTargetException 
... 
Caused by: java.lang.IllegalArgumentException: URL must not be empty

on the line where the URL is created

Upvotes: 2

Views: 1645

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

You are trying to locate the image resource with name "backButton.png" whereas it should be "logo.png".

If the problem persists, try as

<Image url="@../graphics/logo.png" />

since from official FXML tutorial:

The location resolution operator (represented by an "@" prefix to the attribute value) is used to specify that an attribute value should be treated as a location relative to the current file rather than a simple string.

Upvotes: 2

Related Questions