http://tinypic.com/r/25rcxa1/8
Hello everyone. I' am having trouble having my picture being viewable within the JButton. Why isn't this compiling? I have placed image in src folder as you can see from the picture of my dual monitor setup,which shows folder hierarchy and eclipse IDE.
Actually the code compiles, and you have a runtime exception. That's because you pass "../geek.jpg" to the ImageIcon constructor, and that's not how resource resolution works.
You should use "/geek.jpg" instead, provided the image will also be packaged along with your app (check your Eclipse deployment/packaging configuration)
Related
In a JavaFX application i'm developing I use several icons to style some buttons and view objects. The first icons I used were displayed and packed to the deployed files (.dmg with .app and .exe) successfully, since I had to tell Ant to include the resources folder with the icons.
When I run the project with Eclipse, all the icons and images are displayed right, but when I deploy the project, only two of them (the last two included in the last improvement) are not shown. Why could be this, since I didn't change any folder or configuration?
When I get the deployed .app, I right click on it and clicking "show package contents" I can see that not only these two icons are not packed there in /Contents/Java/Resources/, but many others that mysteriously are actually shown in the application. For now I solved this copying all the required icons there, but I can't do this with the .exe generated file, which doesn't show these 2 correct icons.
My application is a music organizer/player called Musicott, is on github so you can see all the code there. The two icons are the slider thumb used to visualize and move through a track; and the default cover image for tracks that doesn't have one.
In this image you can see the problem better http://i.stack.imgur.com/8aVcz.png
Code where I set the default cover image at /com/musicott/view/RootLayoutController.java lines 503-506
Code where I set the default cover image in the play queue list at
/com/musicott/view/PlayQueueController.javalines 98-101
Code where I set the slider thumb icon in
/com/musicott/SceneManager.javalines 245-249
Thanks in advance. I will answer any doubt about the code and the project.
Nice looking project! So, as James_D pointed you are currently loading your icons with the following code:
new Image("file:resources/images/default-cover-icon.png")
new Image("file:resources/images/default-cover-icon.png", 45, 45, true, true));
The problem is that when you pack your application the "file" protocol is no longer valid (remember you are sending a URL to the Image constructor). You could send instead an InputStream (you can get one from the current Class), see this sample code:
new Image(RootLayoutController.class.getResourceAsStream("images/default-cover-icon.png"));
Now, if you use the code exactly like that you will need to make sure (after building the jar) that the "images" folder ends up inside the "com/musicott/view/" folder (since the RootLayoutController class is inside that package), so you will have to add the two extra "com" and "musicott" folders inside your current "resources" folders and move the "images" folder there.. you will probably need to add this in your pom.xml file too (to make sure you export the resources since I think maven will only export by default the "resources" folder located at "src/main/resources"):
<resource>
<directory>resources</directory>
</resource>
Note that if you want to keep your current folder structure (without creating the "com/musicott/view" folders inside the "resources" folder) you can change the path in the getResourceAsStream() call to "/images/default-cover-icon.png" (slash at the start of the string) to indicate that the images folder will be at the root.
Keep up the good work!
I have a little problem with Swing (or maybe netBeans), I was making a testing JFrame, I changed its background by an image with this line of code:
this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src/testdesign/BG.png")))));
And I added a JLabel with an Icon to the frame using netBeans.
The result when compiling the project (In the IDE) was:
All is fine.. But when I generate a JAR file, the result is:
I'm not a Java pro, so I think that the way I changed the background isn't the proper way, or maybe it's another thing.
You use incorrect way to load background image. In your code image location depends on project directory src. You should load image from classpath but file system. You Class.getResource method to fix it e.g.:
this.setContentPane(new JLabel(new ImageIcon(getClass().getResource(pathToImage))));
I have a problem which appears when I export to runnable jar.
Basically I am creating a JavaFx Game which loads Images by using:
Image image = new Image(BlueCar.class.getResource("../resources/blueCar.png").toExternalForm());
Everything works fine within eclipse. However when I try to export and run it give a error on the line of code above.
http://i.imgur.com/VbmGSO3.jpg
I am using MVC Pattern for the application so I have packages separating the class from the images resources:
http://i.imgur.com/gzm7d0s.jpg
Please if you could help know what I am doing wrong please ?
Jar filesystem does not support relative paths - use absolute ones getClass().getClassloader().getResource("path/to/resource/blueCar.jpg")
I realize this question has been asked in various ways, but I couldn't fix my problem despite this. I'm sure I've done everything correctly and yet it still throws the null pointer exception. Is this a Windows 7 issue with Eclipse?
I'm attaching a screen shot so you can see exactly how I have things laid out and that it isn't working still. I really want to trouble shoot this so I can move on in the tutorial I'm doing. This is so frustrating to be hinged on something so trivial!! My image star.png is located inside the image folder which is in the src folder where the package resides as well. Thank you!
To load an image from within an Eclipse project:
Create a new Source Folder. Do not use a regular folder like the
one already in your workspace.
Create a new package named "star3.images" in the folder you just created.
Copy the images you want (in your case, "star.png") into the package.
Load it using the following:
ImageIcon ii = new ImageIcon(getClass().getResource("images/star.png"));
For all other images, replace "star.png" with the name of whatever image you wish to load.
project; properties; java build path; libraries; add class folder
Add a class folder (called res or something) into the project folder then use:
new ImageIcon(this.getClass().getResource("/res/star.png")).getImage();
I was writing a small application and when I tried to create an ImageIcon I always got an exception. The exception was caused by this line of code:
prayerLevel.setIcon(new ImageIcon(getClass().getResource("/icons/icon_prayer.png")));
Now within my program, the folder /icons/ does exist. I don't know if it makes the difference but the class file is within a package, where as the icons folder is within the project folder (when you would see the bin and src folder).
I have looked around for a bit and I couldn't find a solution that could help me solve the problem. Perhaps any of you guys could help?
Edit: someone asked for my folder hierarchy:
I know the class file is not in the same folder as the icons are, but I've made applications where I had to load files from a different folder and doing /folder/ always used to work.
Edit 2:
System.out.println(getClass().getResource("/icons/icon_prayer.png") == null);
Prints true.
I believe the NPE is being thrown from the ImageIcon constructor as getResource is returning null.
Try the following:
getClass().getClassLoader().getResource("/icons/icon_prayer.png")
Or:
ClassLoader.getSystemResource("/icons/icon_prayer.png")
As far as I know getResource() will look into locations of known resources, in other words if the folder /icons/ is not seen as a resource folder it will not as you had expected. There are two ways of going around this as far as I know:
1) Set icons folder as a resource to the application, then you can use getResource() for instance
URL css_url = getClass().getResource("/resource/style.css");
For more info on this option, see http://lj4newbies.blogspot.com/2008/03/using-classgetresource-load-resource.html
2) Get the icon as a regular file without using getResource() method. This is actually adviced in Swing tutorials on Sun/Oracle own documentation .
Generally, applications provide their own set of images used as part
of the application, as is the case
with the images used by many of our
demos. You should use the Class
getResource method to obtain the path
to the image. This allows the
application to verify that the image
is available and to provide sensible
error handling if it is not. When the
image is not part of the application,
getResource should not be used and the
ImageIcon constructor is used
directly. For example:
ImageIcon icon = new
ImageIcon("images/middle.gif",
"a pretty but meaningless splat");
Hope this helps, good luck!
Old thread but since I bumped into a similar problem just now...
I'm using Eclipse and I copied a file to the "resources" folder using system commands (cp). However, eclipse threw a NullPointerException because I didn't refresh the "resources" folder. So the file was there but Eclipse didn't see it.
So in Eclipse: "Package Explorer" -> "resources" -> Mouse right click -> refresh. This fixed it for me.
I added my music, images, etc to a folder added to the build path. Then I just used
URL url="CurrentClass".class.getClassLoader().getResource("media file name not the path");
setIconImage(new ImageIcon(url.getPath()).getImage());
to set the image icon.
The only thing that can throw a NullPointerException in this line of code is the first ., which means that prayerLevel is null.