Java AWT

Abstract Window Toolkit - Java's original GUI framework

🔧 What is Java AWT?

AWT (Abstract Window Toolkit) is Java's original GUI framework that provides basic windowing, graphics, and event-handling capabilities. It uses native system components for platform-specific appearance and behavior.


// Simple AWT Application
import java.awt.*;
import java.awt.event.*;

public class HelloAWT extends Frame {
    public HelloAWT() {
        setTitle("Hello AWT");
        setSize(300, 200);
        
        Label label = new Label("Welcome to AWT!");
        add(label);
        
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new HelloAWT();
    }
}
                                    

Output:

Welcome to AWT!

Key AWT Concepts

🖼️

Frame

Top-level window container

Frame frame = new Frame("Title");
frame.setSize(400, 300);
🎛️

Components

Basic UI elements

Button btn = new Button("Click");
Label lbl = new Label("Text");
📐

Layout Managers

Arrange components automatically

frame.setLayout(new FlowLayout());
frame.setLayout(new BorderLayout());
🎨

Graphics

Drawing and painting capabilities

Graphics g = getGraphics();
g.drawString("Hello", 50, 50);

🔹 Basic AWT Components

Essential components available in AWT:

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

public class AWTComponents extends Frame {
    public AWTComponents() {
        setTitle("AWT Components Example");
        setLayout(new FlowLayout());
        
        // Text components
        Label label = new Label("Enter your name:");
        TextField textField = new TextField(20);
        TextArea textArea = new TextArea(5, 30);
        
        // Buttons
        Button button = new Button("Submit");
        Checkbox checkbox = new Checkbox("Remember me");
        
        // Choice (dropdown)
        Choice choice = new Choice();
        choice.add("Option 1");
        choice.add("Option 2");
        choice.add("Option 3");
        
        // List
        List list = new List(3);
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        
        // Add components to frame
        add(label);
        add(textField);
        add(button);
        add(checkbox);
        add(choice);
        add(list);
        add(textArea);
        
        // Window closing event
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        setSize(400, 400);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new AWTComponents();
    }
}

🔹 Event Handling in AWT

Handling user interactions with event listeners:

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

public class AWTEvents extends Frame implements ActionListener {
    private TextField textField;
    private Label resultLabel;
    
    public AWTEvents() {
        setTitle("AWT Event Handling");
        setLayout(new FlowLayout());
        
        Label inputLabel = new Label("Enter text:");
        textField = new TextField(20);
        Button button = new Button("Process");
        resultLabel = new Label("Result will appear here");
        
        // Register event listener
        button.addActionListener(this);
        
        // Add components
        add(inputLabel);
        add(textField);
        add(button);
        add(resultLabel);
        
        // Window closing
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        setSize(350, 150);
        setVisible(true);
    }
    
    // Handle button click
    public void actionPerformed(ActionEvent e) {
        String text = textField.getText();
        resultLabel.setText("You entered: " + text);
    }
    
    public static void main(String[] args) {
        new AWTEvents();
    }
}

🔹 Graphics and Drawing

Creating custom graphics with AWT:

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

public class AWTGraphics extends Frame {
    
    public AWTGraphics() {
        setTitle("AWT Graphics Example");
        setSize(400, 300);
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        setVisible(true);
    }
    
    // Override paint method for custom drawing
    public void paint(Graphics g) {
        // Set color and draw shapes
        g.setColor(Color.RED);
        g.fillRect(50, 80, 100, 60);
        
        g.setColor(Color.BLUE);
        g.fillOval(200, 80, 100, 60);
        
        g.setColor(Color.GREEN);
        g.drawLine(50, 200, 300, 200);
        
        g.setColor(Color.BLACK);
        g.setFont(new Font("Arial", Font.BOLD, 16));
        g.drawString("AWT Graphics Demo", 120, 250);
    }
    
    public static void main(String[] args) {
        new AWTGraphics();
    }
}

🔹 AWT vs Swing

Understanding the differences between AWT and Swing:

AWT Characteristics:

  • Heavyweight: Uses native system components
  • Platform-dependent: Look varies by operating system
  • Limited components: Basic set of UI elements
  • Faster: Direct native component access

When to use AWT:

  • Simple applications with basic UI needs
  • When you want native look and feel
  • Legacy system compatibility
  • Custom graphics and drawing applications

🧠 Test Your Knowledge

What does AWT stand for?