How to set icon for java swing applciation?

I am trying to customize the icon for my java swing application with this code

setIconImage(new ImageIcon("logo.png").getImage());

but it shows an error on ImageIcon because it cannot find the character. Can someone help me with a solution?

+3
source share
2 answers

Put the file logo.pngin the same package as the class calling it

ProjectRoot
         src
             MyClass.java
             logo.png

and use

ImageIcon icon = new ImageIcon(getClass().getResource("logo.png"));
setIconImage(icon.getImage());

For more information, see Exclude Image Image Icon .

+4
source

try the following:

setIconImage(new ImageIcon(Main.class.getResource("logo.png").getPath()).getImage());

in this example Mainis the name of your class

+1
source

All Articles