How to bind images - icon in .jar file?

UPDATED: 27 May 2013
                        This is really easy task however its seems hard for beginners. You can bind *.jpg, *.png, *.gif in .jar file. I didn't try other file types. If some other file type is working comment it in the article. Anyways this comes in picture when you are dealing with Graphical User Interface (GUI) application. All you need to put images, icons, etc... in your project.

Hoping you are using package phenomenon in your project. It shows neat and clean programming style. It'll help you and other team member to find proper class in proper package. Below is the image of my test project for javaquery.

Java Project Structure

I used com.javaquery.images package to place all my required images. Lets roll for the code... 

How to set button, menu item, label, frame image in netbeans?

Step 1: Store image in proper package.
Step 2: [If required: Goto Windows > Properties . It'll show properties of object in sidebar.] Select object and go to icon properties.


Step 3: Click on browse. Select image from proper package.


How to set image in non IDE code?
 IDE handles the basic properties of object but if you are working on some other editor that don't provide check the below code to set the image for that object.

// jButton1 is my object so change it to yours and also change the path of image in getResource("path_to_image");
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/javaquery/images/Redo.gif")));

Subscribe on social media for more stuffs.
Related Articles:
[How to create .jar file in netbeans? : http://www.javaquery.com/2011/12/how-to-create-jar-file-in-netbeans.html]
[How to Open file by double clicking on it in our .jar file? : http://www.javaquery.com/2010/12/how-to-open-file-by-double-clicking-on.html]
[How to get .java file from .class file? : http://www.javaquery.com/2011/08/how-to-get-java-file-from-class-file.html]

0 comments :