Home Java Example Java Swing Setting an Icon for a Frame in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Setting an Icon for a Frame in Java

Advertisement
In this section, you will learn how to set an icon for the frame in Java Swing.

Setting an Icon for a Frame in Java

     

In this section, you will learn how to set an icon for the frame in Java Swing.

This program helps us to set the icon (image) on the title bar of the frame. When you open frame or window the icon situated on the title bar is seen on the taskbar also. For this purposes, various methods as follows has been used:

frame.setIconImage(Toolkit.getDefaultToolkit().getImage("icon_confused.gif"));
Above method sets the icon for the frame or window after getting the image using the Image class method named
getImage().

frame.getDefaultToolkit():
This is the method of the Toolkit class which gets the default toolkit.

Here is the code of program:

import javax.swing.*;
import java.awt.*;

public class SettingIconFrame{
  public static void main(String[] args){
  JFrame frame = new JFrame("Setting an Icon for a frame");
  frame.setIconImage(Toolkit.getDefaultToolkit()
  .getImage
("icon_confused.gif"));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(400,400);
  frame.setVisible(true);
  }
}

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Ask Questions?    Discuss: Setting an Icon for a Frame in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
mnv
June 11, 2013
reply

@ nisha the image file should be in same folder as that of your file.
dileep
May 1, 2012
i coudn't see anything while i doing same

pls replay ..........related to the problem
nisha
June 25, 2012
icon setting

i hv a question.. where icon file should be located?
DMCA.com