JavaFX SceneBuilder ImageView not working - java

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]

Related

intellij java desktop application how to use resources

I am busy with a java desktop application in intellij. I am struggling to get a image to display in a JLabel
Here is my current code (in a class extending JPanel):
icon = new ImageIcon(getClass().getResource("resources/icon.png"));
lblIcon.setIcon(icon);
Here is a picture of my project structure:
The image is in the resources directory and the screen in in the screens\jpanel.java directory
If I rememeber correctly, IntelliJ automatically adds the contents of the folder marked as "resources root" to the root of your compiled project.
getClass().getResource("...") expects a path relative to your classloader, thus you simply need to provide the name of your image in this case:
icon = new ImageIcon(getClass().getResource("/icon.png"));
lblIcon.setIcon(icon);

Change again the launcher logo in Android Studio 2.1.1

When I first built my Android application the launcher logo was automatically set to this:
android:icon="#mipmap/ic_launcher"
which shows the Android logo. Now I changed it (with the instruction of this link) to a flashlight picture. If I try to run my app everything is working fine and it is the right launcher logo (flashlight).
But I decided to take a new launcher logo, so I deleted the whole new folder which was created in my mipmap folder. At first, I changed the android:icon in the AndroidManifest.xml back to the ic_launcher.
But if I try to run the app again, my launcher logo is still the flashlight. But it should be the Android logo. What did I do wrong?
I followed the instruction again and tried to add a new .png file, but now not even the new mipmap-folder is created.
Is there something else I need to consider?
I could solve the problem by myself!
I did the right-click on the app-folder
I chose New -> Image Asset
I checked the Image Checkbox as Asset Type and selected my desired path
BUT:
I forgot to review the Name. So ic_launcher was still selected!
That's why I got the following errors:
An icon with the same name already exists and will be overwritten.
And...
Some existing files will be overwritten by this operation. Files which replace existing files are marked red in the preview above.
So what you need to do is simply change the name of your image to something that is not flashlight or ic_launcher (i.e. flashlighttest , flashlight2 or ic_launcher2). Just make sure you will not overwrite an existing file!

Background Image shows in SceneBuilder but not when App runs

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.

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

Reading Assets in Eclipse

I am developing a little game where I use some pictures for the sprites etc etc.
It works just fine when I load it from the disc like this
Image image = new Image("C:\\AppleMan.png");
but how can I load it from a foloder within the project. I am using eclipse as IDE and the language is Java :)
I took a screenshot of a sample project so you can see how I have importet the picture
So I want to load the picture from that resource folder like this pseudo code
Image image = getResource("Resources/AppleMan.png");
but that simply doesn't work.
Any help appreciated :)
1) You should add Resources folder to classpath
2) You should locate file absolutely, i.e. "/Resources/AppleMan.png"
P.S.
3) Sorry, also note that getResource returns URL: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String
You're passing a relative path when you were passing a absolute path before. Add the path of the Eclipse workspace.
For example, if your workspace is at C:\Workspace, you need to put
Image image = getResource("C:\\Workspace\\HowToGetResources\\Resources\\AppleMan.png");

Categories

Resources