Abstract Window Toolkit(AWT)

AWT contains large number of classes and methods that allows you to create and manage graphical user interface ( GUI ) applications, such as windows, buttons, scroll bars,etc. The AWT was designed to provide a common set of tools for GUI design that could work on a variety of platforms. The tools provided by the AWT are implemented using each platform's native GUI toolkit, hence preserving the look and feel of each platform. This is an advantage of using AWT.But the disadvantage of such an approach is that GUI designed on one platform may look different when displayed on another platform.

AWT is the foundation upon which Swing is made i.e Swing is a set of GUI interfaces that extends the AWT. But now a days AWT is merely used because most GUI Java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature.


AWT Hierarchy

heirarchy of component class


Component class

Component class is at the top of AWT hierarchy. Component is an abstract class that encapsulates all the attributes of visual component. A component object is responsible for remembering the current foreground and background colors and the currently selected text font.


Container

Container is a component in AWT that contains another component like button, text field, tables etc. Container is a subclass of component class. Container class keeps track of components that are added to another component.


Panel

Panel class is a concrete subclass of Container. Panel does not contain title bar, menu bar or border. It is container that is used for holding components.


Window class

Window class creates a top level window. Window does not have borders and menubar.


Frame

Frame is a subclass of Window and have resizing canvas. It is a container that contain several different components like button, title bar, textfield, label etc. In Java, most of the AWT applications are created using Frame window. Frame class has two different constructors,

Frame() throws HeadlessException 

Frame(String title) throws HeadlessException 

Creating a Frame

There are two ways to create a Frame. They are,

  1. By Instantiating Frame class
  2. By extending Frame class

Creating Frame Window by Instantiating Frame class

import java.awt.*;
public class Testawt 
{
 Testawt()
 {
  Frame fm=new Frame();         //Creating a frame.
  Label lb = new Label("welcome to java graphics");     //Creating a label
  fm.add(lb);		              //adding label to the frame.
  fm.setSize(300, 300);               //setting frame size.
  fm.setVisible(true);                //set frame visibilty true.
 }
 public static void main(String args[])
 {
  Testawt ta = new Testawt(); 
 }
}

creating Frame Window


Creating Frame window by extending Frame class

package testawt;

import java.awt.*;
import java.awt.event.*;

public class Testawt extends Frame 
{
  public Testawt() 
 {
  
  Button btn=new Button("Hello World");		
  add(btn); 		//adding a new Button.
  setSize(400, 500);        //setting size.
  setTitle("StudyTonight");  //setting title.
  setLayout(new FlowLayout());	 //set default layout for frame.	  	
  setVisible(true);           //set frame visibilty true.	  	
  
 }
 
 public static void main (String[] args)
 {
  Testawt ta = new Testawt();   //creating a frame.
 }
}


awt example


Points to Remember:

  1. While creating a frame (either by instantiating or extending Frame class), Following two attributes are must for visibility of the frame:
    • setSize(int width, int height);
    • setVisible(true);
  2. When you create other components like Buttons, TextFields, etc. Then you need to add it to the frame by using the method - add(Component's Object);
  3. You can add the following method also for resizing the frame - setResizable(true);