Background Image shows in SceneBuilder but not when App runs - java

Already searched for an answer for this but the only similar question I could find was here (JavaFX background-image works in scenebuilder and win but not on osx) and it hasn't been answered.
I'm trying to get a background image to show when my App runs. I'm using Eclipse, JavaFX and SceneBuilder. I have applied the CSS File to my FXML file. At the minute the background image will only show in SceneBuilder.
CSS Code:
#background {
-fx-background-position: center;
-fx-background-color: #BDBDBD;
-fx-background-image: url('./Background.png');
-fx-background-repeat: stretch;
}
Anyone got any idea on why this is?
P.S. I have already used two different paths to get to the image, both the image file location within my project and the image file location on my laptop.

you can try this code
.background {
-fx-background-position: center;
-fx-background-color: #BDBDBD;
-fx-background-image: url('file:src/Background.png');
-fx-background-repeat: stretch;
}

You can use the Absolute Path from IntelliJ IDEA.
For that simply follow the steps:
Right click on the image from your IntelliJ IDEA directory.
Click on Copy Path/Reference...
Select the Absolute Path. Only clicking on that usually copies the link. But you can also Ctrl + Shift + C to copy the path.
Now simply paste the absolute path to the FXML file in the image source.
Make sure to use an additional slash \ in the directory slashes as it needs to slash to specify that it is part of that address, not any escaping character.
Please keep in mind that these procedures apply only when you have already copied the image file inside the project folder. If you want to use images located in a different directory, then you have to copy the absolute path. If you are using the Windows operating system, you can simply copy the absolute path by right clicking on the image file and going into the properties. The Location section contains the path.
If the Location does not contain the absolute path including the file name then you simply need to add the file name manually.
For example, check the image below.
Here, in this file's properties tab, the file name is not included in the Location section. Therefore, I need to add the filename at the end of the location/path manually.
In this case, my absolute path would be C:\Users\fahim\Pictures\Screenshots\Screenshot_20230221_110058.png.
If I want to use this path in the FXML file, I need to add one more slash there after each slash in the location. Then the path that I will include in my FXML file is this: C:\\Users\\fahim\\Pictures\\Screenshots\\Screenshot_20230221_110058.png.
Also, if you do not use any extra slashes ( \ ), that will also work perfectly!
For example, check the following code.
<image>
<Image url="C:\Users\fahim\Desktop\aoop-uiu\SceneBuilder_01\FirstApp\src\image\background.jpg" />
</image>
I did not use any extra slash ( \ ) in the absolute path now, but it still works like earlier.

Related

Changing background image with Button press Javafx

I want to change the background image from a scene.
This is from the css file.
.SettingsGrid {
-fx-background-image: url('Background.quiz.jpg');
/*-fx-background: #ffffff;*/
-fx-background-size: cover;
}
I tried using this:
SettingsGrid.setStyle("-fx-background-image: url('Background2.quiz.jpg')");
But instead of the background changing to the new image, it just went white.
Just changing the background colour works.
Try specifying the fully qualified path relative to the root of the classpath.
The resolution path is different depending on whether you access a CSS URL resource from a style sheet or an inline style.
Read the uri documentation in the CSS reference. It is well-written, with clear examples that will help you understand your issue. Quoting from the documentation for CSS URL resolution:
If the address does not have a scheme: component, the address is considered to be the path component only.
A leading / character indicates that the path is relative to the root of the classpath.
If the style appears in a stylesheet and has no leading / character, the path is relative to the base URI of the stylesheet.
If the style appears in an inline style, the path is relative to the root of the classpath (regardless of whether or not there is a leading /).
Example
-fx-background-image: url('Background.quiz.jpg');
When written inside a CSS stylesheet, this will look for an image location in the classpath adjacent to the stylesheet.
When written in an inline style, it will look for an image location in the root of the classpath.
Instead, for the inline style URL, if you specify the full path to the image relative to the root of the classpath, then the image will be found.
If you package your app as a jar, then run jar tvf on the packaged jar, and your image is located, for instance, in the location image/Background2.quiz.jpg, then the URL to use in an inline style would be:
settingsGrid.setStyle("-fx-background-image: url('image/Background2.quiz.jpg')");

jar file to exe losts pictures

I made the following code to put in pictures to my java program:
private String ICET = "." + File.seperator + "Bilder/ICET.jpg"
//some code
label.setIcon(new ImageIcon(ICET));
//some code
It does, that the label has an IconImage and it works in every Workspace with a folder "Bilder", which is situated in the same folder as the program.
It works as a jar-file too, but if I convert the jar-file to an exe using launch4j, the program does not show the pictures.
use
new ImageIcon(getClass().getResource("path"));
path -> if is same folder put only name image : "ICET.jpg"
Most likely, the working directory set when started as an exe differs from when it is started as a jar-file, and thus your program looks at the wrong place for your "Bilder" folder.
You could try this out by printing the absolute path for it, e.g. like this:
System.out.println(new File(".").getAbsolutePath());
To fix the issue, you might try to set the chdir-option in the launch4j-config to . (a single dot). According to the docs:
<chdir>
Optional. Change current directory to an arbitrary path
relative to the executable. If you omit this property or leave it
blank it will have no effect. Setting it to . will change the current
dir to the same directory as the executable. .. will change it to the
parent directory, and so on.
But in the end, you should load the images from the class path, and not just from a folder. If you read them from the class path, they should hopefully be integrated into the exe, otherwise they are not and might get missing when users copy to exe to a new location, making your program no longer functioning.

JavaFX SceneBuilder ImageView not working

I need to create a GUI with SceneBuilder. I added an ImageView to my interface and set the path to my image correctly. The image is showing inside SceneBuilder, but when I run my application, the image is not there.
I put the image inside "img/placeholder.png", and then directly into my root directory. Doesn't matter where I put it, it isn't working.
The path to my gui.fxml file:
/src/gui/gui.fxml
The path to my image file:
/placeholder.png
Can anybody help me please?
There's an easier solution to your problem, without the need of adding or removing any code.
Once you had created an ImageView control in Scene Builder and chosen an image from your computer, select the "gear" icon right beside the ellipses button and select "switch to absolute path".
Image will then automatically appear when you run the code.
Another solution in some particular context (maven related)
Given /src/main/resources/foo/gui.fxml and /src/main/resources/foo/img/foo.png, if you create an image view in SceneBuilder and set the image to foo.png, the url reads #img/foo.png. Unfortunately the image does not show in the application. If you change the image url to #/foo/img/foo.png then the image won't show anymore in SceneBuilder but it will show in the application.
SceneBuilder 8.2.0, JDK 1.8, IntellJ IDEA 2016.1
There should be # as following in image URL. Put image file in inside project folder either same package or different package.
<Image url="#../image/profile_pic.png" />
Just try to refresh Eclipse's System Explorer. I solved it this way.
Path From Content Root
src/main/resources/assets/POMME.png =
<Image url="#../../../assets/POMME.png" />
This is because you haven't added jfoenix jar in your program.
Add jfoenix jar to your project
[xdezo.com][1]

JavaFX usage of "in jar" images in css

I am trying to add background image to some pane in my javafx application, the image is located inside the package resources.img and the css is located inside the package resources.css
if i setting the background image programmatically like suggested in this thread:
Setting background image by javafx code (not css)
it works fine but i would like to know if i can set the background image from the css file itself.
i tried
-fx-background-image: url("#../img/myImage.png");
but then the css interpreter didn't find the image file.
is it possible to do what i want via css?
JavaFX CSS parser has a limitation like
#-keyword statements are ignored.
described in "Limitations" section of CSS Reference Guide. It may be the cause of your problem though didn't confirm myself. Can you try like this: -fx-background-image: url("../img/myImage.png");
or -fx-background-image: url("resources/img/myImage.png");.
Perhaps someone it will be useful.
If you specified: -fx-background-image: url("resources/img/myImage.png");.
It's possible that standalone .jar file will successfully load the image while IDE(like Eclipse) won't. It because IDE alters path and will search something like: C:/My_project/resources/css/resources/img/myImage.png.
It means root for specified path will folder with css file instead of .jar package

How to use background image in CSS?

I am using the background image in my application in CSS . I am using the following code to get the background image file
background-image: url("Image\GREEN.GIF");
but I am not able to get the Image .. I know that it is path problem .. I can able to get the
absolute path in java using
path=new File("web/CSS/myimage.gif").getAbsolutePath();
but in CSS how to get the absolute path..
please help me in getting the absolute path in CSS
Thanks in Advance
Raj
url("Image\GREEN.GIF")
should be
url("Image/GREEN.GIF")
Relative URLs should work perfectly well in CSS, so you don't need absolute URLs. Just be aware that some URLs are relative to the stylesheet, not the HTML that loads it.
I'm not sure there is a way to get the absolute path via CSS, but can you not use relative paths here?
Say you have the following:
-root
-----css
-----img
In order to then reference your images from you css file, you would simply put a path of
background-image: url("../img/myfile.gif");
Also remember that the item your giving the background image to must have content in it, or set dimensions! Otherwise nothing will show!
You don't need absolute URLs.Normally,the urls in stylesheet are relative to the stylesheet,but if you define the mouse cursor's image,URLs relative to the stylesheet are not supported by IE,you should set the path relative to root path or use absolute path.

Categories

Resources