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.)

3 comments

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.

0

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.(ImageIcon.java:138) at com.rvd.webcamwatcher.About1.$$$setupUI$$$(About1.java) at com.rvd.webcamwatcher.About1.(About1.java:23) at com.rvd.webcamwatcher.InitialWindow.showAboutBox(InitialWindow.java:194) at com.rvd.webcamwatcher.InitialWindow.]]>(InitialWindow.java:90)
at com.rvd.webcamwatcher.WebCamWatcher.main(WebCamWatcher.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

0

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.

0

Please sign in to leave a comment.