Sunday, 8 June 2014

Set Icon Image in Java application (Remove default Icon) - NetBeans

For demonstrating purpose I'll create a Project name BloggerEx and in that I have Blogger.java (jFrame)

First of all in your jFrame,

1. In Navigator bar select jFrame, double click it and go to properties or properties tab will show on right side of the NetBeans application.
(You have to select design view, otherwise navigator bar won't show.



If you run the application it will show the default java icon on the upper-left corner.
Now select iconImage property from the properties window.





2. Select the 'iconImage' property and it will show the separate window to change Image icon.
    Select Custom code.





(If you don't have specific icon to set, choose now and copy that icon to project folder,
go to src folder in the project,
select the project name folder, (If you create project name 'My' this folder will be 'my')
Paste the icon in to the folder)

3. Now type the Code as below.
    Note: when you typing the code it may not give the code suggestion sometimes

new javax.swing.ImageIcon(getClass().getResource("/bloggerex/cowboy.png")).getImage()

   Note: ("/bloggerex/cowboy.png") = bloggerex is my project name and cowboy.png is the icon name.
             You can change those with your project name and icon name.

4. Click ok and run the project or jFrame.




Enjoy coding!...
If you have any questions pls feel free to ask :)


















7 comments:

  1. I give up - why is it so difficult to change the icon??? What folder am I putting the picture in? You do not really specify.

    ReplyDelete
  2. ahem. IT DOES NOT WORK IN JAVA 1.8 OR FURTHER WHY

    ReplyDelete
  3. It works but my logo's are in a folder. not in the base project how do refer to this folder to get the Icon out of there

    ReplyDelete
    Replies
    1. project-root/
      ├── src/
      │ ├── main/
      │ │ ├── java/
      │ │ │ └── tu/paquete/PdfFrame.java
      │ │ └── resources/
      │ │ └── icons/
      │ │ └── firma-digital32.png
      ├── pom.xml

      Delete
    2. setIconImage(new ImageIcon(getClass().getResource("/icons/firma-digital32.png")).getImage());

      Delete
  4. Thanks it works.. i put the img.png inside my package in src , n finally simply put type new javax.swing.ImageIcon(getClass().getResource("img.png")).getImage()

    ReplyDelete