Overview of AWT

 

🪟 AWT (Abstract Window Toolkit) in Java 

📌 What is AWT?

AWT (Abstract Window Toolkit) is Java’s original platform-independent GUI (Graphical User Interface) toolkit, part of the java.awt package. It allows developers to build graphical applications like forms, buttons, text fields, and windows.

✅ AWT is part of the Java Foundation Classes (JFC).


🧱 AWT Architecture

AWT is built on top of native code using the system's GUI libraries. So when you use AWT, you're actually calling underlying OS GUI components, which makes AWT platform-dependent in look and feel (called heavyweight components).


🔧 Key Features of AWT

  • Provides basic GUI components like Button, Label, TextField, etc.

  • Event-driven programming model.

  • Uses native system components (heavyweight).

  • Supports layout managers for arranging GUI components.


🔢 Core Classes and Interfaces in java.awt

Component ClassDescription
Frame                Main window
Button                Push button
Label                Text label
TextField                Single-line text input
TextArea                Multi-line text area
Checkbox                Checkbox
CheckboxGroup                Radio buttons
List                Scrollable list
Choice                Dropdown menu
Canvas                For custom drawing
Panel                Container to hold other components
Scrollbar                Vertical/horizontal scroll bar

📋 Example: Simple AWT Application

import java.awt.*; import java.awt.event.*; public class MyAWTApp { public static void main(String[] args) { Frame f = new Frame("My First AWT Window"); Label label = new Label("Enter Name:"); label.setBounds(50, 50, 100, 30); TextField tf = new TextField(); tf.setBounds(150, 50, 150, 30); Button btn = new Button("Click Me"); btn.setBounds(100, 100, 80, 30); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello, " + tf.getText()); } }); f.add(label); f.add(tf); f.add(btn); f.setSize(400, 200); f.setLayout(null); f.setVisible(true); // Close window on clicking 'X' f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { f.dispose(); } }); } }

📚 Layout Managers

AWT uses layout managers to control the positioning of components:

Layout ManagerDescription
FlowLayout            Left to right, wraps at end
BorderLayout            North, South, East, West, Center
GridLayout            Equal-sized grid cells
CardLayout            One component visible at a time
GridBagLayout            Flexible grid (complex)
Example:
f.setLayout(new FlowLayout());

🎯 Event Handling in AWT

AWT uses Event Delegation Model to handle user interactions.

Key Interfaces:

  • ActionListener – for buttons

  • ItemListener – for checkboxes, lists

  • KeyListener – for keyboard events

  • MouseListener / MouseMotionListener

Example:


btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Handle click } });

⚠️ Limitations of AWT

LimitationDescription
Platform Dependent        Uses native components (inconsistent look)
Limited widgets        Lacks rich components (like trees, tables)
Heavyweight        Not lightweight like Swing or JavaFX

✅ When to Use AWT?

  • For simple GUI applications

  • When performance and native look and feel are desired

  • In legacy applications

However, for more modern applications, Swing or JavaFX is recommended.



Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Inheritance and Polymorphism