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')");
Related
So I need to set an icon for Intellij IDEA plugin, but when I'm trying to get this icon from my project with new File(relative path) or getClass().getResource(relative path). It can't find the files, only works with the absolute path. I have tried with the following relative paths:
images/icon.png
resources/images/icon.png
main/resources/images/icon.png
src/main/resources/images/icon.png
Icons path: src/main/resources/images/icon.png
Source code path: src/main/java/com/timetrack/plugin/MyClass.java
code:
File file = new File("src/main/resources/images/running.png");
BufferedImage img = ImageIO.read(file);
or with this
BufferedImage img = ImageIO.read(getClass().getResource("images/running.png"));
EDIT
Need to mention that I'am using Gradle to build the project. So the output directory looks like this:
Icon path: build/resources/main/images/icon.png
Compiled classes:
build/classes/java/main/com/timetrack/plugin/MyClass.class
Your resource string needs to start with a slash, since it is not in the same package as your class.
Paraphrased from the documentation of getResource:
If the name begins with a /, then the absolute name of the resource is the portion of the name following the /.
Otherwise, the absolute name is of the form modified_package_name/name, where the modified_package_name is the package name of this class with / substituted for ..
In other words, the argument passed to Class.getResource is assumed to be in the same package as the Class itself, unless the argument starts with a slash.
This is because the proper way to include resources in an application or library is to place them in the same package directory as the class that uses them. The reason for doing this is the same reason we use packages. For that matter, it’s the same reason applications don’t store all their files in the user’s home directory or in C:\: because there is a real risk that other programs will choose the same name and will interfere with your program.
Class.getResource searches the classpath for the requested resource. If you package your icon as images/running.png, and a library also decides to package its image as images/running.png, then Class.getResource will search the classpath and return whatever it finds first. Depending on the order of the classpath entries, either you will get the wrong image, or that other library will. The two are essentially stepping on each other.
On the other hand, if you place your image at src/main/resources/com/timetrack/plugin/running.png, it’s unlikely any other code will be using that package, so your odds of a collision are mimimal. And because this was intended to be the most common use case, using it this way is easier: You can retrieve the image URL with just MyClass.class.getResource("running.png").
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.
Firstly, is it legal to use the dark theme of JavaFX's SceneBuilder 2 in my application? Since it is open source now here: SceneBuilder/css
Secondly, how to do this if legal? Or just for training purposes if not legal?
I tried to download the ThemeDark.css file from the link above and add it to my fxml file, but i see no change applied.
Any ideas ?
Edit (what I did):
I have downloaded the .css file and pasted it in package css.
Then I added these lines in my .fxml file (with the <> symbols but I removed them in this question as they hid the text if present) :
stylesheets
URL value="#/css/ThemeDark.css"
/stylesheets
See below resulting screenshot: (themeDark not applied)
If you are using SceneBuilder you can add a global css sheet to it and be happy with the new look.
Alternative, if you want to set it within your code you can use
scene.getStylesheets().clear();
scene.getStylesheets().add("path/stylesheet.css"); // Modify to your path
to add a style-sheet. Notice that you should put your style-sheet in a resource folder in your application, just to keep everything cleaned up.
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
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.