Search code examples
javaswingiconsjframe

How to set Icon to JFrame


I tried this way, but it didnt changed?

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

Solution

  • 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.