Associating an icon with JLabel

I don't really understand where the icon image needs to be placed in the project directory hierarchy for it to be able to be associated with a JLabel in the UI designer.

Does it have to be in a jar file? If so, how does one create a jar file for one lonesome icon?

Is there any other way to associate an icon with a JLabel when the "Forms Designer" is used to create the UI? -- perhaps using ImageIcon in the associated java file (which does not require the image to be in a jar.)

Comment actions Permalink

There's no need to put the icon into a jar file. Just store it anywhere in your classpath and select the file from within the UI designer.

Comment actions Permalink

Well, I did that and it displays fine in the UI Designer. However, I get the following runtime error. Is there something else needed for the image to display at runtime?

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.( at com.rvd.webcamwatcher.About1.$$$setupUI$$$( at com.rvd.webcamwatcher.About1.( at com.rvd.webcamwatcher.InitialWindow.showAboutBox( at com.rvd.webcamwatcher.InitialWindow.]]>(
at com.rvd.webcamwatcher.WebCamWatcher.main(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.intellij.rt.execution.application.AppMain.main(

Comment actions Permalink

Looks like the problem causing the nullpointer exception in ImageIcon was caused by a slash in front of the image name. All is well now. Thanks for the help.


Please sign in to leave a comment.