25

I tried this way, but it didnt changed?

ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico");
frame.setIconImage(icon.getImage());
mKorbel
  • 108,320
  • 17
  • 126
  • 296
vijay
  • 1,079
  • 6
  • 21
  • 34
  • There seems to be some problem in your file path. Default image icon will be set if not image found in the path specified. – prasanth Mar 27 '13 at 11:24

12 Answers12

43

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.

Joop Eggen
  • 96,344
  • 7
  • 73
  • 121
  • 2
    i am recieving null pointer exception all time even though Image path is correct – vijay Mar 27 '13 at 12:48
  • 2
    A late response: the icon should be in the same jar as the class on which you call getClass(), and the path is case-sensitive using `/`. – Joop Eggen Apr 16 '13 at 13:09
  • Do you know how to get the favicon from a weburl and to show at setIconImage – Abedin.Zhuniqi Jan 17 '19 at 17:40
  • @Spritzig The favicon is by default under `/favicon.ico`. It can be specified in the HTML head, something like ``. Do a search. Hopefully it actually is a .png, not an .ico. – Joop Eggen Jan 17 '19 at 21:39
  • Thanks but I am working with android studio, can you check my question. https://stackoverflow.com/questions/54233815/how-to-use-bitmap-to-show-a-favicon-of-an-web-for-a-viewholder – Abedin.Zhuniqi Jan 17 '19 at 22:06
  • @Spritzig removing the query string "?..." and adding "/favicon.ico" might often do, if it were not for the Windows .ico format. Sometimes it is a .png in disguise. Often one has to parse the HTML if the site specifies favicon.png or such. That is some work to do. Consider a built-in list of frequent used sites. – Joop Eggen Jan 18 '19 at 07:37
  • Can you provide an answer or code for my question ? – Abedin.Zhuniqi Jan 18 '19 at 10:37
  • @Spritzig sorry, a .ico can be downloaded manually, from /favicon.ico or inspecting the HTML for a link to an other path. If it is .ico I don't think java can turn it into an ImageIcon. I would have answered your linked question if I could, but this requires from you some investigation into both problems: .ico and link. – Joop Eggen Jan 18 '19 at 10:58
  • @JoopEggen Thank you for your time, I appreciate that. – Abedin.Zhuniqi Jan 18 '19 at 11:27
  • 1
    Yes my issue was that I was using a `ico` file. Switched to PNG and it worked! – JFreeman Apr 16 '19 at 07:47
  • 1
    @JFreeman one point: a .ico can contain several images in different resolutions. The [setIconImages](https://docs.oracle.com/javase/9/docs/api/java/awt/Window.html#setIconImages-java.util.List-) can be used for several (single-file) images too; for the larger alt-tab icon etc. I did not mention earlier to not further confuse the issue. – Joop Eggen Apr 16 '19 at 08:12
7

Try putting your images in a separate folder outside of your src folder. Then, use ImageIO to load your images. It should look like this:

frame.setIconImage(ImageIO.read(new File("res/icon.png")));
4

Finally I found the main issue in setting the jframe icon. Here is my code. It is similar to other codes but here are few things to mind the game.

    this.setIconImage(new ImageIcon(getClass().getResource("Icon.png")).getImage());

1) Put this code in jframe WindowOpened event

2) Put Image in main folder where all of your form and java files are created e.g.

src\ myproject\ myFrame.form
src\ myproject\ myFrame.java
src\ myproject\ OtherFrame.form
src\ myproject\ OtherFrame.java
src\ myproject\ Icon.png

3) And most important that name of file is case sensitive that is icon.png won't work but Icon.png.

this way your icon will be there even after finally building your project.

Airy
  • 4,656
  • 6
  • 42
  • 67
2

This works for me.

    frame.setIconImage(Toolkit.getDefaultToolkit().getImage(".\\res\\icon.png"));

For the export jar file, you need to configure the build path to include the res folder and use the following codes.

    URL url = Main.class.getResource("/icon.png");
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
Joe
  • 21
  • 3
1

Yon can try following way,

myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
Lucifer
  • 28,605
  • 21
  • 86
  • 137
Nilesh Jadav
  • 804
  • 9
  • 7
1

Here is the code I use to set the Icon of a JFrame

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

  try{ 
    setIconImage(ImageIO.read(new File("res/images/icons/appIcon_Black.png")));
  } 
  catch (IOException e){
    e.printStackTrace();
  }
0

Just copy these few lines of code in your code and replace "imgURL" with Image(you want to set as jframe icon) location.

JFrame.setDefaultLookAndFeelDecorated(true);

//Create the frame.
JFrame frame = new JFrame("A window");

//Set the frame icon to an image loaded from a file.
frame.setIconImage(new ImageIcon(imgURL).getImage());
Matsemann
  • 18,825
  • 18
  • 54
  • 88
0

I'm using the following utility class to set the icon for JFrame and JDialog instances:

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Scanner;

public class WindowUtilities
{
    public static void setIconImage(Window window)
    {
        window.setIconImage(Toolkit.getDefaultToolkit().getImage(WindowUtilities.class.getResource("/Icon.jpg")));
    }

    public static String resourceToString(String filePath) throws IOException, URISyntaxException
    {
        InputStream inputStream = WindowUtilities.class.getClassLoader().getResourceAsStream(filePath);
        return toString(inputStream);
    }

    // http://stackoverflow.com/a/5445161/3764804
    private static String toString(InputStream inputStream)
    {
        try (Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A"))
        {
            return scanner.hasNext() ? scanner.next() : "";
        }
    }
}

So using this becomes as simple as calling

WindowUtilities.setIconImage(this);

somewhere inside your class extending a JFrame.

The Icon.jpg has to be located in myproject\src\main\resources when using Maven for instance.

BullyWiiPlaza
  • 12,477
  • 7
  • 82
  • 129
0
public FaceDetection() {
    initComponents();
    //Adding Frame Icon
    try {
        this.setIconImage(ImageIO.read(new File("WASP.png")));
    } catch (IOException ex) {
        Logger.getLogger(FaceDetection.class.getName()).log(Level.SEVERE, null, ex);
    }
}'

this works for me.

0

I use Maven and have the structure of the project, which was created by entering the command:

mvn archetype:generate

The required file icon.png must be put in the src/main/resources folder of your maven project.

Then you can use the next lines inside your project:

ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("./icon.png"));
setIconImage(img.getImage());
fedotsoldier
  • 184
  • 14
0

My project code is as below:

private void setIcon() {
       setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/slip/images/cage_settings.png")));

    }
Kayvan Tehrani
  • 2,570
  • 2
  • 26
  • 43
-1
frame.setIconImage(new ImageIcon(URL).getImage());

/* frame is JFrame setIcon method, set a new icon at your frame new ImageIcon make a new instance of class (so you can get a new icon from the url that you give) at last getImage returns the icon you need it is a "fast" way to make an icon, for me it is helpful because it is one line of code */

Community
  • 1
  • 1
Dimitris
  • 1
  • 1