I was writing a small application and when I tried to create an ImageIcon I always got an exception. The exception was caused by this line of code:
prayerLev
As far as I know getResource()
will look into locations of known resources, in other words if the folder /icons/
is not seen as a resource folder it will not as you had expected. There are two ways of going around this as far as I know:
1) Set icons folder as a resource to the application, then you can use getResource()
for instance
URL css_url = getClass().getResource("/resource/style.css");
For more info on this option, see http://lj4newbies.blogspot.com/2008/03/using-classgetresource-load-resource.html
2) Get the icon as a regular file without using getResource()
method. This is actually adviced in Swing tutorials on Sun/Oracle own documentation .
Generally, applications provide their own set of images used as part of the application, as is the case with the images used by many of our demos. You should use the Class getResource method to obtain the path to the image. This allows the application to verify that the image is available and to provide sensible error handling if it is not. When the image is not part of the application, getResource should not be used and the ImageIcon constructor is used directly. For example:
ImageIcon icon = new ImageIcon("images/middle.gif", "a pretty but meaningless splat");
Hope this helps, good luck!