I've recently finished a project I'm working on, and part of it involves using ImageIcons to fill JLabels. So my question is about how I would go about making the filepaths more universal. By that I mean rather that having the path to my image being something like "C:/users/user/documents/project/file.jpg" for example. I'm looking to pack it into a .jar file and include the target folders/files in a zip with the jar.
Asked
Active
Viewed 89 times
1 Answers
0
In the Using Swing Components tutorial the Loading Images Using getResource section shows seven scenarios in which the following code should works:
java.net.URL imageURL = myDemo.class.getResource("images/myImage.gif");
...
if (imageURL != null) {
ImageIcon icon = new ImageIcon(imageURL);
}

taringamberini
- 2,719
- 21
- 29
-
1To clarify further, you can store the image files inside a package, such as `com.myapp.images` You can then refer to them using `getResource("com/myapp/images/img_1.gif")` and so on. Note that the "." in the package name becomes a "/" when you want to load the resource. – wavicle Mar 21 '14 at 22:03