How to set Icon to JFrame

vijay picture vijay · Mar 27, 2013 · Viewed 82.2k times · Source

I tried this way, but it didnt changed?

ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico");
frame.setIconImage(icon.getImage());

Answer

Joop Eggen picture Joop Eggen · Mar 27, 2013

Better use a .png file; .ico is Windows specific. And better to not use a file, but a class resource (can be packed in the jar of the application).

URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());

Though you might even think of using setIconImages for the icon in several sizes.