0

i have a JavaFX project with many packages. i wanted to create a folder with all my icons within. the icon path is: src/icon/test.png and my class, where i try to initialize my Image, is: src/project/menus/ressources/settings/SettingWindow.java. my problem is, that i am not able to get to the root folder, into the icon folder.

here is my source for the SettingWindow:

package project.menus.ressources.settings;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class SettingWindow{

    @FXML private TextField nameTF;
    @FXML private Button pinButton;

    private Stage stage;


    public void setStage(Stage stage) {
        this.stage = stage;

/*-----------------Here where i try to initialize the Image -------------*/
        Image icon = new Image("file: /icon/test.png", 25,25, false, false);

        pinButton.setGraphic(new ImageView(icon));
    }
/*-----------------------------------------------------------------------*/ 
    public TextField getNameField() {
        return this.nameTF;
    }


}

"file: /icon/test.png" is not the only thing i tried. i found somewhere a solution to get the root directory with getRoot() but i couldnt use this method. I cant use the AbsolutPath to the folder, because its planed to use this software on different PC's

Dubi
  • 33
  • 3
  • The String "file: /icon/test.png" is not a valid URL, nor is going to be a valid location on the file system. – KevinO Oct 20 '18 at 01:59
  • Possible duplicate of [Cannot load image in JavaFX](https://stackoverflow.com/questions/16099427/cannot-load-image-in-javafx) – SedJ601 Oct 20 '18 at 02:22
  • If your icons are _resources_ you should not be using a `file:` URL. – Slaw Oct 20 '18 at 06:18

2 Answers2

0

In my experience, the most flexible approach for finding resources is to use an InputStream and allow the classloader to find the object. So, based upon the statement that the resource (the image) is in the src folder, and presuming that said src folder is fully added to the classpath, then something like the following might work.

Note: I am assuming the .setGraphic(new ImageView(icon)) is correct -- I'm not overly familiar with JavaFX.

private void loadAndDisplayImage(Button btn) {
  final String name = "icon/test.png";
  ClassLoader cl = this.getClass().getClassLoader();

    //
    // use the try with resources, and allow the classloader to find the resource
    //   on the classpath, and return the input stream
    //
    try (InputStream is = cl.getResourceAsStream(name)) {
      // the javafx Image accepts an inputstream, with width, height, ratio, smoot
      Image icon = new Image(is, 25, 25, false, false);

      // should probably ensure icon is not null, but presumably if the resource
      //   was found, it is loaded properly, so OK to set the image
      btn.setGraphic(new ImageView(icon));
    }
    catch (IOException e) {
      e.printStackTrace();
    }
}

Then call the loadAndDisplayImage(pinButton); method from the setStage(...)

I think this approach is a bit more flexible than trying to code the URL.

KevinO
  • 4,303
  • 4
  • 27
  • 36
0

Do not expect the source directory to be available at runtime the same way they were before building your program.

Often the compilation results are compressed with the resources to a jar file. The contents of a jar file are not accessible via the file system. Instead you should use a Class.getResource get the url of a resource and pass it to the Image constructor. (This only works if the resources are available via classpath, but if they are available, it works whether they are packed in a jar or not):

Image icon = new Image(SettingWindow.class.getResource("/icon/test.png").toExternalForm(),
                       25, 25, false, false);
fabian
  • 80,457
  • 12
  • 86
  • 114