Setting the Icon for the frame in Java

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

Setting the Icon for the frame in Java

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

Setting the Icon for the frame in Java

Setting the Icon for the frame in Java

     

Introduction

Here, you will learn how to set the icon of the frame.

This program is the detailed illustration to set the icon to the frame. Toolkit class has been used to get the image and then the image is used to display as frame icon. Toolkit class is used to bind the various components to particular native toolkit implementations. 

getDefaultToolkit():
This method returns the default toolkit.

getImage(url or filename):
This method retrieves the pixels data of the image which can be either in format of .gif, .jpeg or .png. If the security manager installed then the getImage() method first check whether the specified file name has the permission to use and then retrieves the image in pixels format.

Here is the code of the program : 

import java.awt.*;

public class AwtSetIconOfFrame{
public static void main(String[] args){
Frame frame = new Frame("Icon setting for the Awt Frame");
Image icon = Toolkit.getDefaultToolkit().getImage("icon_confused.gif");
Label lbl = new Label("Welcome in Roseindia.net Tutorial.",Label.CENTER);
frame.add(lbl);
frame.setSize(400,400);
frame.setIconImage(icon);
frame.setVisible(true);
  }
}

Download this example.