How to Change Jframe Icon

How to change JFrame icon

Create a new ImageIcon object like this:

ImageIcon img = new ImageIcon(pathToFileOnDisk);

Then set it to your JFrame with setIconImage():

myFrame.setIconImage(img.getImage());

Also checkout setIconImages() which takes a List instead.

How to set Icon to JFrame

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.

How to change java icon in a JFrame

I have an answer for you. First, make sure that the images are in a folder, not a package. Next, insert this line of code:

Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("path/to/image.png"));
ImageIcon icon = new ImageIcon( );
setIconImage(icon.getImage());

This code gets the image from the class path, and returns it as a image icon, and then it sets it. This should add the image icon to the application. If it doesn't, then tell me.

EDIT: After you told me that that didn't work then I decided to take a second crack at it...
First, put your images into a completely separate folder. I usually call this /res. Next, put your image in there. Now, for loading I took a completely different route. I decided to use ImageIO instead of default loading. To load the image, you use this code:

try {
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
}
catch (IOException exc) {
exc.printStackTrace();
}

ImageIO works a lot better for loading images. If this still doesn't work then please tell me.

If you want to export this as a JAR then put a folder the same name as you used in the program in the same directory as the JAR.

How to change the size of JFrame icon

Frame icons are set according to a size decided by the OS.

  • If you supply icons of various sizes, Windows will use the smaller one for the frame icon, and the larger one as the image to include in the window shown when the user types alt tab to change between apps.
  • OS X shows no frame icon at all.

See also: Sizes of frame icons used in Swing.

Setting an icon for a jFrame in Netbeans swing gui builder

NVM, I found a solution on: http://www.youtube.com/watch?v=o_35iro4b7M

Describing how to set the icon and title of a jFrame. Basically, it requires
the libraries

import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Image;
import javax.imageio.ImageIO;

I pretty much wanted to stick with using Netbean's guibuilder for now, at least for prototyping.

setImageIcon doesn't set JFrame icon on mac swing window

Mac does not support frame icons, as seen in this answer.



Related Topics



Leave a reply



Submit