1

I am in the middle of a code/application and I've gotten to the point where I am releasing it publicly. Id like to know a simple example code of how I can "set" the ICON image for the application. Just a simple code where I can place at the top of my class that will grab the Icon image out of its directory [/res/Icon.png]

Thanks <3

Jahshua
  • 55
  • 2
  • 8

1 Answers1

3

You can use Frame#setIconImage(Image) or if your want something that is a little more flexible, Window#setIconImages(List)

As demonstrated by

Credit to those authors please

Updated with simple example

Loading images by it's nature can raise problems. You need to be prepared for the fact that it might fail. Hopefully, you've prepared the application well enough that under normal operations, this would be a rare situation

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FrameIconTest {

    public static void main(String[] args) {
        new FrameIconTest();
    }

    public FrameIconTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");

                try {
                    List<Image> icons = new ArrayList<Image>(5);
                    icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon16x16.png")));
                    icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon24x24.png")));
                    icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon32x32.png")));
                    icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon64x64.png")));
                    icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon128x128.png")));
                    frame.setIconImages(icons);
                } catch (IOException exp) {
                    exp.printStackTrace();
                    // Log the problem through your applications logger...
                }

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JLabel("Frame with icon"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Please expand:D I have my code I just want to place a simple line or 2 of code at the top that will set the Icon image. I tried what you post'ed by the default java icon still appears. – Jahshua Nov 07 '13 at 00:15
  • @Jahshua See the attached links for examples – MadProgrammer Nov 07 '13 at 00:16
  • I do that ^ but on line: ImageIO.read(imgStream); I need to surround with a try and catch or throw declaration. If i surround try and catch i need to set the myImg variable to null, if i throw declaration like iOS Exception it ruins the whole code. – Jahshua Nov 07 '13 at 00:30
  • So, regardless of how you load images, you are going to need to handle the possibility that it might throw an exception. How you deal with that will depend on your needs – MadProgrammer Nov 07 '13 at 00:52