11

I have been searching everywhere on how to set the icon image in Java, and it always ends up not working or it gives me errors. Here, in my main method is where I put the code:

public static void main(String[] args) {
    Game game = new Game();

    // This right here! 
    game.frame.setIconImage(new ImageIcon("/Icon.png").getImage());

    game.frame.setResizable(false);
    game.frame.setTitle(title);
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);

}

My path for the image is "%PROJECT%/res/Image.png" and I just use /Image.png to go ahead and access my res folder (as I have done in other parts of my project) I have even converted it into an icon file, and tried that, but all it decides is to use the default Java icon.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Shzylo
  • 303
  • 1
  • 3
  • 13
  • 2
    This gets asked about once every 16 hours. You might want to search in future. – Andrew Thompson Jun 29 '13 at 19:28
  • +1 to @AndrewThompsons comment. Have a read [here](http://stackoverflow.com/questions/13796331/jar-embedded-resources-nullpointerexception/13797070#13797070). it should help you through most problems of embedded resources in your jar. – David Kroukamp Jun 29 '13 at 19:33
  • Please note that I've deleted your [tag:set] tag since your question has nothing to do with the programming concept of "sets" or collections in general. I've added the [tag:Swing] tag as this heading is very relevant to your question. – Hovercraft Full Of Eels Jun 29 '13 at 19:38

4 Answers4

10

Your problem is often due to looking in the wrong place for the image, or if your classes and images are in a jar file, then looking for files where files don't exist. I suggest that you use resources to get rid of the second problem.

e.g.,

// the path must be relative to your *class* files
String imagePath = "res/Image.png";
InputStream imgStream = Game.class.getResourceAsStream(imagePath );
BufferedImage myImg = ImageIO.read(imgStream);
// ImageIcon icon = new ImageIcon(myImg);

// use icon here
game.frame.setIconImage(myImg);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • when I use this it throws an error at setIconImage, _The method setIconImage(Image) in the type JFrame is not applicable for the arguments (ImageIcon)_ and it says to change it to setIconImages or to change type ImageIcon to Image. – Shzylo Jun 29 '13 at 20:02
  • thank you that worked :) just had to put some stuff in a try/catch. – Shzylo Jun 29 '13 at 21:07
8

Use Default toolkit for this

frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
Jayram
  • 18,820
  • 6
  • 51
  • 68
1

I use this:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class IconImageUtilities
{
    public static void setIconImage(Window window)
    {
        try
        {
            InputStream imageInputStream = window.getClass().getResourceAsStream("/Icon.png");
            BufferedImage bufferedImage = ImageIO.read(imageInputStream);
            window.setIconImage(bufferedImage);
        } catch (IOException exception)
        {
            exception.printStackTrace();
        }
    }
}

Just place your image called Icon.png in the resources folder and call the above method with itself as parameter inside a class extending a class from the Window family such as JFrame or JDialog:

IconImageUtilities.setIconImage(this);
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
0

The below method works well on Java 7 and above.

JFrame frame = new JFrame("MyAPP");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
    frame.setIconImage(ImageIO.read(YourClass.class.getResourceAsStream("/icon.png")));
} catch (IOException ex) {
    ex.printStackTrace();
}
frame.setVisible(true);

Park your icon.png image file to /src/main/resources.

Googlian
  • 6,077
  • 3
  • 38
  • 44