Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Path for an ImageView using Javafx and Maven

Tags:

fxml

javafx-8

I am designing a JavaFX program, using zenjava basic archetype. Everything works just find, except when I want to add and Image using an ImageView widget.

I have created a new folder under src/main/resources called images where I store the image I want to show on my ImageView.

so, according to this image is stored in /src/main/resources/images/image.jpg

The thing is that I open SceneBuilder and drag and drop the image from the folder C:\Users\Toni\workspace\MyProject\src\main\resources\images\image.jpg and I can see the image on the screen. I save the fxml file and then, I open it and the fxml file refers to the image as follows:

<ImageView fitHeight="200.0" fitWidth="200.0" layoutX="1463.0" layoutY="551.0">
     <image>
        <Image url="@../images/image.jpg" />
     </image>
  </ImageView>

However, when I compile it and run it, this error appears:

[INFO] --- javafx-maven-plugin:8.1.2:run (default-cli) @ QAdmin ---
[INFO] Running JavaFX Application
null/../images/wait_time_icon.png
Exception in Application start method
[WARNING] 
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
... 1 more
Caused by: javafx.fxml.LoadException: 
unknown path:57

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at app.source.general.MainApp.start(MainApp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1100)
at javafx.scene.image.Image.<init>(Image.java:681)
at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:763)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 11 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1092)
... 17 more

Obviously, the reference to the image @../images/image.jpg is not working properly.

So, my questions are:

Am I placing the image resources where it is supposed to be? How can I do it to reference this image properly using fxml so when I run the app it will find the image file?

Thank you!

like image 483
Asaak Avatar asked Oct 20 '22 02:10

Asaak


1 Answers

Assuming the following locations:

src/main/java/ - for the Java classes
src/main/resources/fxml/view.fxml
src/main/resources/images/image.png

You should be able to reference image.png from view.fxml using:

<Image url="@/images/image.png" />
like image 68
Chris Dev Avatar answered Oct 22 '22 22:10

Chris Dev