If the image is in the package foo.bar.baz
, you use
getClass().getResource("/foo/bar/baz/the-icon.png")
as explained in the javadoc, of course.
Question
Ok so I use use this code to import an icon from the same file that the class is in and assign it a JLabel
,
JLabel label1;
Icon iron_ore = new ImageIcon(getClass().getResource("icon_ore"));
label1 = new JLabel(iron_ore);
But what code do I have to add if I want to put the icon in a file different from witch the class is in? Thank you.
Solution
If the image is in the package foo.bar.baz
, you use
getClass().getResource("/foo/bar/baz/the-icon.png")
as explained in the javadoc, of course.