• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

putting image files to Jar File in Swing application

 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I have a bit problem with my image in my GUI project.

I have a method to retrieve ImageIcon like this:

public static ImageIcon createImageIcon(Class sourceClass, Stringpath) {
URL imageURL = sourceClass.getClass().getResource(path);

if (imageURL != null) {
return new ImageIcon(imageURL);
} else {
System.err.println("Couldn't find file : + path");
return null;
}
}

Where should I put the image files in JAR file assuming all my images is in the directory named images?


Thanks for your answer

Jeffry Kristianto Yanuar
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeffry Kristianto Yanuar:
URL imageURL = sourceClass.getClass().getResource(path);

Since sourceClass is already a Class object, sourceClass.getClass will return Class.class. Just remove the getClass() call.

Where should I put the image files in JAR file assuming all my images is in the directory named images?

If the images directory is put in the root of the JAR file, you can access them by using "/images/myimage.gif" for the path. If the images directory is in the same folder as the class file, just remove the leading /
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, it works!!!


Jeffry Kristianto Yanuar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic