Table of contents
1.
Introduction
2.
What is Java AWT?
3.
Why is Java AWT is Platform Dependent?
4.
Java AWT Hierarchy
4.1.
Components
4.2.
Containers
4.3.
Layout Managers
4.4.
Event Handling
4.5.
Graphics and Drawing
5.
Core AWT Components in Java
6.
Types Of Containers in Java AWT
6.1.
1. Window
6.2.
2. Dialog
6.3.
3. Panel
6.4.
4. Frame
7.
Java AWT Example
7.1.
Example 1: By Extending the Frame class (inheritance)
7.1.1.
Output
7.2.
Example 2: By creating an instance of Frame class (association)
7.2.1.
Output
8.
Useful Methods of Component Class in Java AWT
9.
Event Handling in AWT
10.
Limitations of AWT
11.
Frequently Asked Questions
11.1.
Where is Java AWT used?
11.2.
What is Swing and AWT in Java?
11.3.
What are the 4 types of containers in Java AWT?
11.4.
What is the difference between Java AWT and JavaFX?
11.5.
What is the use of the window class?
11.6.
How to change a button from enabling to disabling after a click?
11.7.
What are the Advantages of AWT in Java?
11.8.
Where is Java AWT Used?
12.
Conclusion
Last Updated: May 31, 2025
Easy

Java AWT Tutorial

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Abstract Window Toolkit (AWT) is a Java API for creating graphical user interfaces (GUIs). It provides a set of classes and interfaces for building components like buttons, labels, and text fields, as well as containers like windows and panels. AWT follows an event-driven programming model and is platform-dependent. It serves as the foundation for more advanced GUI toolkits like Swing. This article will talk about Java AWT Basics, AWT Hierarchy, its components, and containers with the help of different examples.

Java AWT Tutorial

What is Java AWT?

Java AWT (Abstract Window Toolkit) is an API used to create GUI or window-based Java programs. Since Java AWT components are platform-dependent, they are shown by the operating system's view. It is also heavyweight, suggesting that its components use the Operating System's resources. The Java.awt package contains classes for the AWT API. TextField, CheckBox, Choice, Label, TextArea, Radio Button, List, and so on.

Why is Java AWT is Platform Dependent?

Java AWT uses native platform (OS) subroutines to create components such as text boxes, checkboxes, and buttons. An AWT GUI with a button, for example, would have a varied appearance and feel across platforms such as Windows, Mac OS, and Unix since these systems have a different look and feel for their native buttons. AWT directly calls their native subroutine that constructs the button. Simply said, an AWT-based programme will seem like a Windows application when launched on Windows but will look like a Mac application when run on Mac OS.

For its platform dependence behaviour and hefty weight, AWT is rarely used nowadays. Because the underlying operating system creates them, AWT components are considered heavyweight (OS). For example, if you instantiate a text box in AWT, you are essentially asking the operating system to generate a text box.

Read More About, Why is Java Platform Independent

Java AWT Hierarchy

The AWT hierarchy in Java classes is given below:

 


Image Source: WebEduClick.com

 

Components

Components include all elements such as buttons, text fields, scrollbars, and so on. As seen in the picture above, AWT has classes for each component. To have everything on a screen in a specific position, we must add them to a container. A container is similar to a screen on which we place components such as buttons, text fields, checkboxes, etc. In a nutshell, a container holds and controls the arrangement of components. Because a container is a component (as seen in the above hierarchy diagram), we may put a container within a container.

Containers

The Container is an AWT component that may hold other components like buttons, text fields, labels, etc. Containers are classes that extend the Container class, such as Frame, Dialog, and Panel. It is simply a screen where the components are positioned in their respective areas. As a result, it includes and governs the arrangement of components.

Layout Managers

Layout Managers are responsible for arranging components within a container. They automatically adjust the size and position of components based on the container’s layout rules. Common types include FlowLayout, BorderLayout, and GridLayout.

Event Handling

Event Handling enables responding to user interactions like button clicks, key presses, or mouse movements. It involves event sources, listeners, and handlers using classes like ActionListener and MouseListener to define specific actions.

Graphics and Drawing

Java AWT provides the Graphics class for drawing shapes, text, and images. It supports methods like drawRect, drawString, and drawImage, allowing developers to create customized visuals and dynamic user interfaces.

Core AWT Components in Java

The Abstract Window Toolkit (AWT) provides essential components for building graphical user interfaces (GUIs) in Java. Here are the core AWT components:

  • Label: Displays a short string or text message.
  • Button: Triggers an action when clicked.
  • TextField: Allows the user to enter a single line of text.
  • TextArea: Allows multiline text input or display.
  • Checkbox: Enables multiple selections from a group of options.
  • RadioButton (CheckboxGroup): Allows only one selection from a group (mutually exclusive).
  • List: Displays a list of selectable items.
  • Choice: A drop-down list of items.
  • Canvas: A blank rectangular area for drawing graphics or handling custom rendering.
  • Panel: A generic container used to group components.
  • Frame: A top-level window with a title bar and border.
  • Dialog: A pop-up window used to interact with the user.

Types Of Containers in Java AWT

1. Window

A Window class instance has no border and no title.

2. Dialog

The dialog class has a border and a title. A Dialog class instance cannot exist without a Frame class instance.

3. Panel

There is no title bar, menu bar, or border in the panel. It is a generic container used to store components. A Panel class instance serves as a container to which components can be added.

4. Frame

A-frame has a title, a border, and menu bars. It may include various components such as buttons, text fields, scrollbars, etc. This is the most often used Container when constructing an AWT application.

Java AWT Example

We may use Frame to design a GUI in two ways:

1) By Extending the Frame class.

2) By making an instance of the Frame class.

Example 1: By Extending the Frame class (inheritance)

import java.awt.*;
// Extended Frame class 
// Theclass "SimpleExample" will behave
// like a Frame


public class SimpleExample extends Frame{
    SimpleExample(){  
        Button b=new Button("Button!!"); 
        
        // setting button position on screen
        b.setBounds(50,50,50,50);  
        
        //adding button into frame 
        add(b); 
        
        //Setting Frame width and height
        setSize(500,300); 
        
        //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.
         */
        setVisible(true);  
    }  
    public static void main(String args[]){  
         // Creating the instance of Frame
         SimpleExample fr=new SimpleExample();  
    }
}

Output


Example 2: By creating an instance of Frame class (association)

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

Output

Try it by yourself on java online compiler.

Useful Methods of Component Class in Java AWT

MethodDescription
public void add(Component c)This method inserts a component on this component.
public void setSize(int width, int height)It helps to Set the size (width and height) of the component.
public void setLayout(LayoutManager m)It defines the layout manager for the component.
public void setVisible(boolean status)It changes the visibility of the component, by default false.

Event Handling in AWT

Event handling in AWT is the mechanism that controls the interactions between the user and GUI components. It uses event listeners to respond to user actions like clicks, typing, or mouse movements. Components generate events, and registered listeners handle these events by implementing specific interfaces, such as ActionListener for buttons or MouseListener for mouse events.

Limitations of AWT

  • Platform Dependence: AWT relies on native GUI components, causing inconsistent look and behavior across different platforms.
  • Limited Components: Offers fewer GUI components compared to newer frameworks like Swing or JavaFX.
  • Poor Customization: Limited ability to customize the appearance and behavior of components.
  • Performance Issues: Slower rendering and event handling compared to lightweight GUI toolkits.
  • Lack of Advanced Features: Does not support rich UI features like drag-and-drop, tables, or trees natively.
  • Complex Layout Management: Layout managers can be hard to use and customize effectively.

Frequently Asked Questions

Where is Java AWT used?

Java AWT is used to create platform-dependent graphical user interfaces (GUIs) for desktop applications with basic components like buttons, text fields, and event handling.

What is Swing and AWT in Java?

AWT is Java’s original GUI toolkit using native OS components, while Swing is a lightweight, platform-independent GUI framework built on top of AWT.

What are the 4 types of containers in Java AWT?

The four main AWT containers are Frame, Panel, Applet, and Window, used to hold and organize GUI components in applications.

What is the difference between Java AWT and JavaFX?

Java AWT is an older GUI toolkit, while JavaFX is a modern, feature-rich framework. JavaFX offers advanced graphics, animations, and multimedia support, whereas AWT provides basic components. JavaFX is more flexible and customizable, while AWT is simpler but less visually appealing.

What is the use of the window class?

The window class may be used to build a simple, bare-bones window without a border or menu. The window class may also be used to display welcome or introduction screens.

How to change a button from enabling to disabling after a click?

When a button is pressed, an action event is triggered, which may be recorded by implementing the ActionListener interface and the actionPerformed(ActionEvent ae) function. You can then press the button. To deactivate this button, use setEnable(false).

What are the Advantages of AWT in Java?

Java AWT provides a platform-independent way to create graphical user interfaces (GUIs). It offers a rich set of pre-built components like buttons and text fields and integrates seamlessly with the underlying native GUI.

Where is Java AWT Used?

Java AWT is used for creating simple GUI applications, lightweight desktop tools, and educational projects. It serves as a foundation for Swing and other frameworks, enabling cross-platform development of graphical interfaces.

Conclusion

In this article, we have extensively discussed Java AWT Basics, and its hierarchy with the help of various examples. We have also brushed up on some of the important java Concepts. Additionally, we have explored important concepts like components, containers, layout managers, event handling, and graphics, solidifying your understanding of AWT.

Recommended Article:

Live masterclass