Results 1 to 9 of 9
- 07-27-2010, 03:02 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
ImageIcon doesnt work after exporting the project
Hey,
in my application i use some ImageIcons for buttons and other things, which really worked fine.
But now i exported my project to a runnable jar file and now the image icons dont get loaded properly anymore.
This is how my project looks like:
Java Code:Projectname --- src ------ myPackage --------- myClassUsesImageIcons --- images ------ myImageForImageIconUse.png
This is the code that gets the ImageIcon:
So basically im trying to get the location of the images folder and then get the file from inside the folder and make an imageicon.Java Code:public static ImageIcon createImageIcon(String fileName) { // bin dir File dir = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()); // install dir File baseDir = dir.getParentFile(); // image directory (installFolder/images) String imageDir = baseDir.getAbsoluteFile() + File.separator + "images"; // image file that is supposed to be loaded File imageFile = new File(imageDir + File.separator + fileName); try { return new ImageIcon(imageFile.toURI().toURL()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.err.println("Couldn't find file: " + fileName); return null; } }
But i guess after exporting the project and having a xyz.jar file, the path to the images folder might look like this: C:\xyz.jar\images
so this obvioulsy can't work :(
But i dont know how to make an image icon using a different approach.
What is important is that the images should be somehow stored in the jar file, so that i can keep the whole programm as a single file.
Please help me with this :)Last edited by random7; 07-27-2010 at 06:30 PM.
- 07-27-2010, 03:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You're trying to load your icons as if they were files; an entry in a .jar file isn't a file itself, i.e. it is just a 'resource'; read all the details in the API documentation for the Class.getResourceAsStream( ... ) method.
kind regards,
Jos
- 07-27-2010, 03:20 PM #3
Do you want the image files outside of the jar file?
Or would putting them in the jar be OK? It makes for easier distribution and maintenance.
Use the URL constructor for ImageIcon and get the URL by using the Class getResource() method.
- 07-27-2010, 04:14 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
yes i want it inside the jar.
ill try the methods you suggested.
thanks for the fast answers :)
- 07-27-2010, 06:29 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
i got it to work now :)
heres my method now. alot smaller now too :D
thanks again :)Java Code:public static ImageIcon createImageIcon(String fileName) { return new ImageIcon(MyClass.class.getResource("/images/" + fileName)); }
- 07-27-2010, 07:23 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 07-27-2010, 07:31 PM #7
Not sure what that means.don't name that parameter 'fileName'; it isn't a file;
What would you call: AnImage.png
On WinXP I'd call 'AnImage.png' the name of a file. What its being used for in a program is another issue. In this case the image in the file is used as an icon.
Probably a waste of time for my opinion on this. Things are slow so I was looking for an excuse to post yet another programmer's opinion. You need at least three for a quorum.
- 07-27-2010, 08:12 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
A resource in a .jar file isn't a file; so if a resource is named AnImage.png and the resource happens to be an icon that name is an icon name; files are just artifacts (just as resource in .jar files), so AnImage.png isn't an ArtifactName or a FileName but simply an icon name.
kind regards,
Jos
- 07-27-2010, 09:00 PM #9
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Not sure what a quorum is, but we now have 3 people :)
I'd say its a file. JAR files are, at some level, archives of FILES. They're like zip files, tar files, etc. And when its not run from a jar, its definitely accessing a file...
Doesn't really matter that much though.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
PrintWriter doesnt work :(
By Addez in forum New To JavaReplies: 11Last Post: 01-17-2010, 05:59 PM -
mysql connect button doesnt work quite right
By 711groove in forum New To JavaReplies: 0Last Post: 12-13-2009, 07:01 AM -
why doesnt my insertion sort method not work?
By Jeremy8 in forum New To JavaReplies: 7Last Post: 11-15-2009, 02:56 AM -
Dll Call doesnt work
By INFACT in forum New To JavaReplies: 1Last Post: 10-04-2009, 09:31 PM -
java doesnt allow vista to work
By 10rosas in forum New To JavaReplies: 5Last Post: 12-22-2008, 04:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks