I am developing a small desktop application using Java(ofcourse…).
Its a very long time that i had my hands on Swing.
First thing was to choose an IDE that will make the job more easier and this time i wanted to try
different IDE(i use Eclipse at work and at home too…), so i ended up with NetBeans.
NetBeans for Java desktop applications is like VisualBasic to Windows applications.
Everything was fine until i wanted to add an image to the title bar of a window(JFrame) .
Since this is a desktop java application, i thought to have the images folder inside the jar file.
Issues i faced
i created a folder with name images and put some images inside it.
Since Netbeans gives an ant build file to build and create a jar file , i had to edit the build.xml file to add the image folder inside myapp.jar file.
i tried my best to do it , but i couldn.
Solution
after creating the image folder , right-click on the project , select properties, click Add Folder and select the images folder.
Now Netbeans rewrites the build.xml file to add the images from the image folder to the jar.
so i edited the build.xml file ,so that the images are under a folder inside the jar.
before editing the build.xml the task was
<target name="-do-compile" depends="init,deps-jar,-pre-pre-compile,-pre-compile" if="have.sources">
<j2seproject3:javac/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.images.dir}" excludes="${build.classes.excludes}"/>
<fileset dir="${src.dir}" excludes="${build.classes.excludes}"/>
</copy>
</target>
after editing the build.xml
<target name="-do-compile" depends="init,deps-jar,-pre-pre-compile,-pre-compile" if="have.sources">
<j2seproject3:javac/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${build.classes.excludes}"/>
</copy>
<copy todir="${build.classes.dir}/images">
<fileset dir="${src.images.dir}" excludes="${build.classes.excludes}"/>
</copy>
</target>
to load the image file to the title bar of the JFrame
public Image getIconImage() {
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("images/login.gif");
return new ImageIcon(imageURL).getImage();
}
setIconImage(getIconImage());
So thats the solution i found to refer to an image file inside images folder packed inside a jar file.
You Should Also Check Out This Post:
- Firefox 3: The Next-Generation Web Browser
- Microsoft Pushes Alternative to AJAX
- Hardy Heron and NVIDIA
- HOWTO install , configure and run Resin web server in Ubuntu/Kubuntu
- Reflection in action
More Active Posts:
- javax.servlet.ServletException: Path loginLayout does not start with a "/" character (6)
- HOWTO install , configure and run Resin web server in Ubuntu/Kubuntu (5)
- Linux ads (3)
- Desktop Java application and image folders (2)
- HOWTO add "diggit" and "del.icio.us" links to blogger( not beta , but latest version) (1)
- HOWTO pass request and response from one servlet to another servlet … (1)
- Java is Everywhere -- cool JAVA ad (1)
- New Features in JDBC 4.0 (1)
- AjaxRain (1)



Sounds Good…It’s good to experience Java Swing too