Results 1 to 9 of 9
  1. #1
    random7 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Exclamation 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:
    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;
    		}
    }
    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.

    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 07:30 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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.

  4. #4
    random7 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    yes i want it inside the jar.

    ill try the methods you suggested.
    thanks for the fast answers :)

  5. #5
    random7 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    i got it to work now :)

    heres my method now. alot smaller now too :D
    Java Code:
    	public static ImageIcon createImageIcon(String fileName) {
    		return new ImageIcon(MyClass.class.getResource("/images/" + fileName));
    	}
    thanks again :)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by random7 View Post
    i got it to work now :)

    heres my method now. alot smaller now too :D
    Java Code:
    	public static ImageIcon createImageIcon(String fileName) {
    		return new ImageIcon(MyClass.class.getResource("/images/" + fileName));
    	}
    thanks again :)
    Good; the method can't get much smaller than that; one little nitpick though: don't name that parameter 'fileName'; it isn't a file; what about 'iconName'?

    kind regards,

    Jos (<--- nitpicker ;-)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    don't name that parameter 'fileName'; it isn't a file;
    Not sure what that means.

    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.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Not sure what that means.

    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.
    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

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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

  1. PrintWriter doesnt work :(
    By Addez in forum New To Java
    Replies: 11
    Last Post: 01-17-2010, 06:59 PM
  2. mysql connect button doesnt work quite right
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 08:01 AM
  3. why doesnt my insertion sort method not work?
    By Jeremy8 in forum New To Java
    Replies: 7
    Last Post: 11-15-2009, 03:56 AM
  4. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 10:31 PM
  5. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 05:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •