Quantcast
Viewing all articles
Browse latest Browse all 5661

How to refer to image resources in javaFX css file to ensure that they are copied into the correct output directory?

Hi all,

 

I am currently writing a JavaFX application and am having trouble correctly configuring the resources. My project, "Project", has a source structure as follows:

 

src/main/java/rootPackageName/

src/main/resources

 

Here the resources directory is marked as a resource directory, while the java directory is marked as the sources directory.

 

I have a .css file in src/main/java/rootPackageName/view/styling.css . This file refers to some images in the resources directory. Using IDEA's autocomplete suggests the path "../../../resources/button.png" for the image file, giving a css entry as below.

 

.button {
    -fx-graphic: url("../../../resources/button.png");
}

 

While this is technically correct, refering to the image file using a relative path like this does not seem to work properly in IDEA. The image files are copied from resources into out/production/*.png , but java looks for the resources in

 

IdeaProjects/Project/out/resources/button.png

 

I believe this is because the path is relative, so from /out/production/rootPackageName/view/ it refers to out/ where it needs to refer to out/production/rootPackageName/resources.

 

I can avoid this problem if I refer to the images absolutely, e.g.:

 

-fx-graphic: url("file:/C:/Users/username/IdeaProjects/Project/src/main/resources/button.png");

 

However this has multiple drawback as first, I cannot use IDEA autocomplete to fill in these suggestions, not to mention that intelliJ gives "cannot resolve directory" error when using this format. The program compiles and runs fine, but it is hugely annoying. There must be a simple solution to this, but I am really at a loss here.

 

So, some questions arise:

 

1) Why does IDEA copy the files from src/main/resources/ into /out/ and not out/production/resources/ ?
2) How can I refer to my image using a directory structure relative to the root of the project, and not relative to the css source file?
3) Alternative, how can I prevent IDEA from giving me so many errors when refering to the absolute path.

 

Many thanks.


Viewing all articles
Browse latest Browse all 5661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>