0

I have tried to find my answer on google but no results right now.

So i'm sorry if its an already solved post.

What I want to do : When my FXML file is load, I want the imageView in this file to load an image from an URL which is contain in a table of Strings who contains differents URLs, so I can switch the image on load according to my choice variable. I have tried with absolute paths and relative paths.

In my main :

//url tab of the 2 images
public static String[] images = new String[2];

//variable that include the choice for the image to load in the tab images
public static int choice = 0;

public MainTest() {
    images[0] = "C:/Users/Stagiaire ACI/Desktop/Java/ImageTestImage/Assasin.png";
    images[1] = "C:/Users/Stagiaire ACI/Desktop/Java/ImageTestImage/Barde.png";
}

My fxml file controler :

@FXML
private ImageView image;

@FXML
private void initialize() {
    Image image = new Image(MainTest.images[MainTest.choice]);
    this.image.setImage(image);
}

And my Fxml file :

<AnchorPane prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.test.view.TestControler">
   <children>
      <ImageView fx:id="image" fitHeight="310.0" fitWidth="319.0" layoutX="306.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true" />
   </children>
</AnchorPane>

So with this i can change choice to change the image on load. My images works well when I load it with the classic way.

Don't even know if it's possible.

Thank you!

EDIT : I have solved my problem by putting file: above the file url.

  • You can't use `@` or `..` in the path for an image in your Java code. The `Image` constructor is expecting a URL of the image (see [docs](https://docs.oracle.com/javase/9/docs/api/javafx/scene/image/Image.html)). – James_D Feb 27 '18 at 13:46
  • I have update my code like I have edit in the question, all in absolute path, but didn't even work. – Atra Wanderer Feb 27 '18 at 13:54
  • In the doc its `Image​(String url)` so Image is waiting a string, and that's what i'm giving to him. `MainTest.images[MainTest.choice]` is a String. – Atra Wanderer Feb 27 '18 at 14:02
  • ["The Image class represents graphical images and is used for loading images from a specified URL."](https://docs.oracle.com/javase/9/docs/api/javafx/scene/image/Image.html). "All URLs supported by URL can be passed to the constructor. If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case." The string you pass, e.g. `"C:/Users/Stagiaire ACI/Desktop/Java/ImageTestImage/Assasin.png"` does not represent a URL (in particular, it will interpret `C` as the URL scheme). – James_D Feb 27 '18 at 14:03
  • So i need to make an URL tab with : `URL url = new URL(yourUrl, "C:/Users/Stagiaire ACI/Desktop/Java/ImageTestImage/Assasin.png");` – Atra Wanderer Feb 27 '18 at 14:10
  • What's wrong with the approaches provided in the answer by fabian? – James_D Feb 27 '18 at 14:10

1 Answers1

1

URL properties with values starting with @ are treated differently by FXMLLoader. They are treated as relative to the document. This does not work outside of fxmls.

Absolute file paths don't work either since they don't contain a protocol.

If your image is a resource you should use Class.getResource and if it's not, use File.toURI().toURL():

images[0] = MainTest.class.getResource("/absolute/package/path/Assasin.png").toExternalForm();
images[1] = new File("C:/Users/Stagiaire ACI/Desktop/Java/ImageTestImage/Barde.png").toURI().toURL().toExternalForm();
James_D
  • 201,275
  • 16
  • 291
  • 322
fabian
  • 80,457
  • 12
  • 86
  • 114
  • I'm not sure to understand the deference between the 2 ways, and when i'm trying both of them, neither works. Is that because I need to make a Tab of File instead of Strings? – Atra Wanderer Feb 27 '18 at 14:13
  • @AtraWanderer "Resource" is something that is part of the project, i.e. bundled with your application. "Not a resource" would be, for example, something on the end user's file system (an image that is not part of the project). – James_D Feb 27 '18 at 14:14
  • My images are located on a source folder so I understand that they are a Resource : https://imgur.com/a/mozlg So they are not in a package, they are in the project file. That's why I don't know what to put in : `"/absolute/package/path/Assasin.png"` – Atra Wanderer Feb 27 '18 at 14:25
  • @AtraWanderer If they are in the root of the classpath, then simply `"/Assasin.png"`. – James_D Feb 27 '18 at 14:34
  • It still makes me an error, am I used to put `MainTest.class.getResource("/Assasin.png").toExternalForm();` into a String tab? – Atra Wanderer Feb 27 '18 at 15:02
  • This answer describes adding a folder with a text file to a java project in eclipse: https://stackoverflow.com/a/27935114/2991525 `getResourceAsStream` and `getResource` work with the same parameter. – fabian Feb 27 '18 at 15:10
  • Have you got an example beacause of what i have read/learn, my file Images in the project file is always load and I just have to read on `/Barde.png` to get it. Realy thank you for the help! – Atra Wanderer Feb 27 '18 at 15:43
  • I have tried to put "../../../Image/Barde.png" to get it, it's the relative path from the mainTest class where is the code `images[0] = MainTest.class.getResource("../../../Image/Assasin.png").toExternalForm();` is. But you sayed that relative paths aren't useable that's it? – Atra Wanderer Feb 27 '18 at 15:52