Java – AWT Tutorial For Beginners

Abstract Window Toolkit is referred to as AWT. For developing Graphical User Interface (GUI) for Java applications, it is a platform-dependent API.

Why is AWT reliant on the platform? When constructing components like a textbox, checkbox, button, etc., Java AWT uses a native platform (Operating Systems) function. For instance, an AWT GUI with a button would have a distinct appearance and feel on platforms like Windows, Mac OS, and Unix since AWT directly calls the native function that constructs the button on each of these systems. Simply said, an AWT-built program will seem like a Windows application when launched on Windows, but will appear as a Mac application when run on Mac OS.

Because of its platform dependence and weight, AWT is no longer often utilized. Since AWT components are produced by the underlying operating system, they are regarded as heavyweight (OS). For instance, when you instantiate a text box in AWT, you are essentially requesting OS to generate the text box on your behalf.

Because it is lightweight and platform neutral, Swing is a favorite API for window-based applications. Although Swing is based on the AWT API, its appearance and feel are independent to the underlying platform. Compared to AWT, it features more robust and adaptable components. Swing has various sophisticated components including tabbed panels, scroll panes, trees, tables, and lists in addition to common ones like buttons, check boxes, and labels. In a different lesson, we will go into more depth about Swing.

AWT hierarchy

Java - AWT Tutorial For Beginners

Components and containers

The term “component” refers to each and every element, including buttons, text fields, scrollbars, etc. Classes for each component are available in AWT, as depicted in the diagram above. We must add them to a container in order to arrange everything on a screen in a specific location. A container is similar to a screen on which we place elements such as buttons, text fields, checkboxes, etc. In other words, a container manages how components are laid out and contains them. A container can be added inside another container because it is a component in and of itself (as shown in the hierarchy diagram above).

Types of containers: As was previously stated, a container is a location where components like text fields, buttons, checkboxes, etc. are added. The four container types offered by AWT are Window, Frame, Dialog, and Panel. Frame and Dialog are subclasses of the Window class, as shown in the hierarchy diagram above.

Window: A window instance lacks a border and a title.
Dialog: The class for dialog contains a border and a title. Without a corresponding instance of the Frame class, a Dialog instance is not possible.
Title bar, menu bar, and border are absent from a panel. It serves as a general-purpose container for holding parts. A Panel class instance offers a container to which components may be added.
Title, border, and menu bars are all features of a frame. It may include a variety of elements, including buttons, text fields, and scroll bars. This is the container that is most frequently used when creating AWT applications.

Java AWT Example

There are two techniques to build a GUI with Frame:
1) By adding to the Frame class
2) By creating a Frame class instance

Let’s examine each one’s example in turn.

AWT Example 1: creating Frame by extending Frame class

import java.awt.*;

/* We have extended the Frame class here,
 * thus our class "SimpleExample" would behave
 * like a Frame
public class SimpleExample extends Frame{
        Button b=new Button("Button!!"); 
        // setting button position on screen
        //adding button into frame 
        //Setting Frame width and height
        //Setting the title of Frame
        setTitle("This is my First AWT example"); 
        //Setting the layout for the Frame
        setLayout(new FlowLayout());
        /* By default frame is not visible so 
         * we are setting the visibility to true 
         * to make it visible.
    public static void main(String args[]){  
         // Creating the instance of Frame
         SimpleExample fr=new SimpleExample();  

AWT example 1

AWT Example 2: creating Frame by creating instance of Frame class

import java.awt.*;
public class Example2 {
      //Creating Frame    
      Frame fr=new Frame();       
      //Creating a label
      Label lb = new Label("UserId: "); 
      //adding label to the frame
      //Creating Text Field
      TextField t = new TextField();
      //adding text field to the frame
      //setting frame size
      fr.setSize(500, 300);  
      //Setting the layout for the Frame
      fr.setLayout(new FlowLayout());
   public static void main(String args[])
       Example2 ex = new Example2(); 

AWT example 2

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button