How do I change JFrame image icon?

Category: javax.swing, viewed: 27412 time(s).

This example demonstrate how to change a JFrame image icon using the setIconImage() method.

package org.kodejava.example.swing;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.io.IOException;

public class FrameIconExample extends JFrame {
    public static void main(String[] args) {
        FrameIconExample frame = new FrameIconExample();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //
        // Set the window size and its title
        //
        frame.setSize(new Dimension(300, 200));
        frame.setTitle("Frame Icon Example");

        //
        // Read the image that will be used as the application icon. Using "/"
        // in front of the image file name will locate the image at the root
        // folder of our application. If you don't use a "/" then the image file
        // should be on the same folder with your class file.
        //
        BufferedImage image = null;
        try {
            image = ImageIO.read(frame.getClass().getResource("/colors.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        frame.setIconImage(image);

        // Display the form
        frame.setVisible(true);
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats