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.

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:

More Active Posts: