How to change java icon in a JFrame
Asked Answered
P

5

10

Ok so I've been researching this one quiet a bit. I am fairly new to java but thought that this one would be easy. Ive tried just about every way that has been answered on this site and still no luck, and usually when I look here I am able to find a answer that fits what I am looking for. Does anyone know how to change the Java icon in the top corner of the JFrame. I'm pretty positive that its not my file path either because all my images are in the same folder and they all work, this is the only one that I can't seem to get to work.

This is the first part my code for the main menu of my program, everything works except when i try to add the icon image. The code I've entered below does not have anything in it for the JFrame IconImage, I removed it since it didn't work. So if there is someone who knows how to get it working with this code that would be highly appreciated, thank you very much in advanced!

public class MainFrame
{
private MyPanel main;
private MyPanel2 create;
private MyPanel3 update;
private MyPanel4 find;
JFrame frame = new JFrame("Main Menu:");

public void displayGUI()
{
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel contentPane = new JPanel();
    contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    contentPane.setLayout(new CardLayout());
    main = new MyPanel(contentPane, this);
    create = new MyPanel2(contentPane);
    update = new MyPanel3(contentPane);
    find = new MyPanel4(contentPane);
    contentPane.add(main, "Main Menu");
    contentPane.add(create, "Create Part");
    contentPane.add(update, "Update Part");
    contentPane.add(find, "Find Part");
    frame.setLocation(200, 200);
    frame.setSize(700, 580);
    frame.setContentPane(contentPane);

    frame.setVisible(true);

}
Persuader answered 23/7, 2013 at 15:50 Comment(4)
Post the relevant code only. We don't want to search through your wall of code for the part where the icon of the frame is changed. Then tell us what you expect this relevant part of the code to do, and what it does instead.Muenster
This is the part of the code that the change icon part should be in. I've tried numerous different ways of entering the code but this is what the code for the JFrame is without the change icon code in it.Persuader
Show your best attempt, and tell us what happens. Hints: docs.oracle.com/javase/6/docs/api/javax/swing/…, docs.oracle.com/javase/6/docs/api/javax/swing/…, docs.oracle.com/javase/tutorial/uiswing/components/frame.htmlMuenster
Nothing happens on every attempt, nothing changes at all its like i never typed anything inPersuader
O
15

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.

Oriole answered 13/8, 2013 at 11:15 Comment(4)
This didn't work for me, I even made sure it was in a folder and not a packagePersuader
Where should i put it in the code i tried it right below frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Persuader
I had the line of code right in front of the frame.setDefaultCloseOperation. This way, we know that the icon is set before anything else happensOriole
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("icons/icon1.png")); ImageIcon icon = new ImageIcon(image); setIconImage(icon.getImage());Overbear
G
4

For example in a NetBeans project, create a resources folder in the src folder.

Put your images (jpg, ...) in there.

Whether you use ImageIO or Toolkit (including getResource), you must include a leading / in your path to the image file:

Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/agfa_icon.jpg"));
setIconImage(image);

If this code is inside your JFrame class, the image is added to the frame as an icon in your title bar.

Gitagitel answered 5/3, 2014 at 8:59 Comment(0)
M
2

This works pretty fine for me. Just add this after you've created your JFrame.

try {
   Image image = new ImageIcon("/icons/image.jpg").getImage();
   frame.setIconImage(image);
}catch(Exception e){
   System.out.println("Application icon not found");
}
Milne answered 14/7, 2015 at 9:16 Comment(1)
Please, avoid code-only answers. You should always provide some explanation of the reason of the problem in the question, possible solutions and example code (this). Spend some time to help people.Fictional
S
1
  1. Paste your image icon (fav.png) in the same package first,
  2. Write following code in constructor of JFrame:

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("fav.png")));

Note:- fav.png is the name of icon

Shattuck answered 29/8, 2017 at 4:50 Comment(0)
F
0
this.setIconImage(new ImageIcon(getClass().getResource("/iconsfolder/iconsname.jpg")).getImage()); 
          // sets the Global icon for the system

try this code put after this code:

public void displayGUI()
{
Faltboat answered 29/8, 2017 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.