Monday, June 21, 2010

Images in NetBeans Swing Applications



I was trying to create a setup package for a software created by someone with NetBeans yesterday. The guy put the images in the NetBeans project directory but not in a package. Everything run fine on his system since the images were created on the various JPanels and JLabels using absolute path. I run it on my machine and booom. The images were not showing. I went back to correct the problem. Absolute paths are the worst thing a programmer should do in development. NetBeans has made our lives so easy we just click, drag-and-drop and everything happens.
The moral of the story - Adding images for platform independence in NetBeans should be done in packages. The best thing in my opinion is to put all the images in an images folder, copy and paste in NetBeans default package for the project in question. Just click the component with which you seek to add images and look up for its Icon property in NetBeans. Use Image Within Project option to browse for images in the project's directory.
If you want to use an External Image it is best to Import to Project for easy accessibility. Note either is a programmer's choice.
What I do most often is create a package for images so that when I package the software, I need not copy a single image. They get embedded in the jar file.

No comments:

Post a Comment