How to Set Icon to Jframe

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 set icon of JFrame in eclipse

Just do it like this:

try {
frame.setIconImage(ImageIO.read(getClass().getResourceAsStream("/icon.png")));
} catch (IOException e) {
e.printStackTrace();
}

Note: Put the image icon in the classpath

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.

Set icon to JFrame

Use ImageIO.getReaderFileSuffixes() for an array of file types that the Java run-time can read. It might appear like this (actual example for this machine right now):

type: bmp
type: jpg
type: wbmp
type: jpeg
type: png
type: gif

Nope. No ico type.

How to put a JFrame icon

Instead of saving your resources alongside your source files, save it outside src in a folder called something like res. It's a good idea to separate your source from your resources.

As for the icon, use setIconImage(ImageIO.read("res/icon.png"));

However:

If your icons are inside a jar, you'll have to use ImageIO.read(Classname.class.getResource("res/icon.png"));

I think that's right, however take care with your relative paths and if it's not working, the first thing to check would the your paths relative to where it's being executed from (whether that's from the .class dirs or from a jar at some stage).

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.



Related Topics



Leave a reply



Submit