vijay
vijay

Reputation: 1139

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());

Upvotes: 28

Views: 106927

Answers (13)

Bico Steve
Bico Steve

Reputation: 1

ImageIcon img = new ImageIcon(Objects.requireNonNull(getClass().getClassLoader()
    .getResource("konashop/com/icon.png"))); // the URL should be within the src with the classes
frame.setIconImage(img.getImage());

Upvotes: 0

Chauncey Lee
Chauncey Lee

Reputation: 95

My project code is as below:

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

    }

Upvotes: 0

fedotsoldier
fedotsoldier

Reputation: 190

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());

Upvotes: 0

Joe
Joe

Reputation: 19

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));

Upvotes: 1

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.

Upvotes: -1

BullyWiiPlaza
BullyWiiPlaza

Reputation: 19185

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.

Upvotes: 0

Asad Bashir
Asad Bashir

Reputation: 1

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());

Upvotes: 0

Dimitris
Dimitris

Reputation: 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 */

Upvotes: -1

Craig Cantrell
Craig Cantrell

Reputation: 9

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();
  }

Upvotes: 0

Nilesh Jadav
Nilesh Jadav

Reputation: 910

Yon can try following way,

myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));

Upvotes: 0

Abdul Jabbar
Abdul Jabbar

Reputation: 5931

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.

Upvotes: 4

user2675678
user2675678

Reputation:

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")));

Upvotes: 6

Joop Eggen
Joop Eggen

Reputation: 109532

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.

Upvotes: 47

Related Questions