0

I am trying to avoid using SceneBuilder to load an image to a scene. In SceneBuilder, I made a spot for the ImageView, and I assigned it an fx id, then declared that fx id as "icon" and did the same for the controller class I would like to call the ImageView/image from the controller class. I have tried

    @FXML
    ImageView icon;

    Image image = new Image("../basketball.jpg");
    icon.setImage(image);

I don't know what the solution would be for this, so is there a simple way to do this without SceneBuilder browsing for the image directly? My .jpg file is at the top of my project, not in a specific folder. I thought it would be simple since I have the .jpg file included in my eclipse project.

  • If the image is located at the root of the classpath then it should be `new Image("/basketball.jpg")`. – Slaw Jul 18 '18 at 01:19
  • did i need to add URL somewhere or anything? Nothing seems to be working for what I have so far – TrigramSage Jul 18 '18 at 02:24
  • Possible duplicate of [Cannot load image in JavaFX](https://stackoverflow.com/questions/16099427/cannot-load-image-in-javafx) – SedJ601 Jul 18 '18 at 03:06
  • Make sure the image is part of the classpath. If you're using a build tool like Maven or Gradle then it should be under the `src/main/resources` directory. If you're using pure Eclipse unfortunately I don't know how Eclipse configures the project directory. – Slaw Jul 18 '18 at 03:17

1 Answers1

0

A image in javafx uses a url so i would try Image image = new Image("file:path\\to\\file.jpg");