Search code examples
javaeclipseimagejavafxfxml

How the image url is wrriten in java fxml


I am working with fxml file and I don't know how to write a url (path) to an image i have , I tried absolote path and directory path

I get an invalid url errors like >>

Feb 08, 2019 5:09:27 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by 
JavaFX runtime of version 10.0.2
null/bars-chart.png
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException: 
unknown path:64

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2450)
at back_end.image_main.start(image_main.java:25)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:688)
at javafx.fxml/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
at javafx.fxml/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:771)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
... 11 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
... 17 more
Exception running application back_end.image_main

my project structure is

fxml_image :
           src :
                front_end :
                           >(bars-chart.png)
                           >(main.fxml)
                back_end :
                           >(image_main.java)

this block is a part of my (main.fxml) >>

    
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
      <children>
        <Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="&#10;" text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
          <font>
            <Font size="18.0" />
          </font>
            </Label>
                <ImageView fitHeight="349.0" fitWidth="606.0" layoutX="12.0"         layoutY="11.0" pickOnBounds="true" preserveRatio="true">
                   <image>
                      <Image url="@bars-chart.png" />
                   </image>
                </ImageView>
          </children>
        </AnchorPane>

this is the java file (image_main)

    package back_end;

    import java.io.FileInputStream;
    import java.io.IOException;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class image_main extends Application  {
    
        public static void main(String[] args) { Application.launch(args); }
        
        @Override 
        public void start(Stage stage) throws IOException { 
        
        // Create the FXMLLoader 
        FXMLLoader loader = new FXMLLoader(); 
        // Path to the FXML File 
        String fxmlDocPath = "src/front_end/main.fxml"; 
        FileInputStream fxmlStream = new FileInputStream(fxmlDocPath);
        // Create the Pane and all Details 
        VBox root = (VBox) loader.load(fxmlStream);
        // Create the Scene 
        Scene scene = new Scene(root); 
        // Set the Scene to the Stage 
        stage.setScene(scene); 
        // Set the Title to the Stage 
        stage.setTitle("A FXML Example which includes FXML Files"); 
        // Display the Stage 
        stage.show();
    

    }}

Is there another way to load the FXML, How image links are interpreted


Solution

  • Paths are resolved using the fxml file's location. If you load the fxml using an InputStream as source for the fxml data, FXMLLoader is unable to determine the location url.

    Specify the location instead:

    FXMLLoader loader = new FXMLLoader(new File("src/front_end/main.fxml").toURI().toURL());
    VBox root = (VBox) loader.load();
    

    Assuming the fxml is a resource, I recomend using getResource instead to get the url:

    FXMLLoader loader = new FXMLLoader(getClass().getResource("/front_end/main.fxml"));