The AWT: Class Hierarchy of Window Fundamentals, Frame & UI Components
1. Introduction to AWT (Abstract Window Toolkit)
AWT (java.awt package) is Java’s original platform-dependent toolkit for creating GUI (Graphical User Interface) window-based applications.
- Why are AWT components called “Heavyweight”? Because AWT components rely directly on the host Operating System’s native GUI widgets (OS peers). Therefore, an AWT
Buttonlooks like a Windows button on Windows and a Mac button on macOS.
2. The Class Hierarchy of Window Fundamentals
Understanding the AWT Class Hierarchy is essential for DAVV exams. At the top of all AWT GUI classes sits Component, which splits into interactive UI widgets and Container classes:
• Window: Top-level window with no borders/title bar.
• Frame: Full standalone window with Title Bar, Borders, Minimize/Maximize/Close buttons & Menu Bar!
• Panel: Borderless sub-container used to group components inside a Frame.
• Applet: Browser-embedded panel.
Key Window Fundamental Classes Explained:
Component: The abstract superclass of every non-menu user interface element (Button,Label,Checkbox, etc.). It defines common methods likesetSize(w, h),setLocation(x, y),setVisible(boolean),setBackground(Color), andsetForeground(Color).Container: A subclass ofComponentthat can hold other components inside it using theadd(Component c)method and arrange them using a Layout Manager (setLayout()).Window: A top-level container that sits directly on the desktop without any border or menu bar (rarely used directly).Panel: The simplest concrete subclass ofContainer. It has no title bar or border of its own; it is used to group smaller components together inside a largerFrame.Frame: A subclass ofWindowthat represents a standard desktop application window complete with a Title Bar, Resizable Borders, Minimize/Maximize/Close buttons, and support for a Menu Bar!
3. Creating a Frame Window in AWT
You can create a GUI window in AWT by extending Frame (or creating an instance of Frame) and using three essential methods:
setTitle(String title): Sets the text in the window’s title bar.setSize(int width, int height): Sets the width and height of the window in pixels.setVisible(true): Makes the frame visible on the screen (by default, frames are invisible!).
4. Basic AWT User Interface Components
All AWT UI components are added to a Frame or Panel using the add(componentObj) method. Here are all 8 UI components explicitly listed in the DAVV syllabus:
1. Label (Read-Only Text)
Displays a single line of uneditable text (used as a caption next to text fields).
- Constructors:
Label(),Label(String text),Label(String text, int alignment)(Label.LEFT,Label.CENTER,Label.RIGHT). - Example:
Label lblName = new Label("Student Name:");
2. Button (Clickable Push Button)
Creates a labeled push button that triggers an ActionEvent when clicked.
- Constructors:
Button(),Button(String label) - Example:
Button btnSubmit = new Button("Submit Form");
3. Checkbox (Multiple Selection Tick Box)
A box that can be either checked (true) or unchecked (false). Used when the user can select multiple hobbies/options at the same time.
- Constructors:
Checkbox(String label),Checkbox(String label, boolean state) - Example:
Checkbox cbJava = new Checkbox("Java", true); // Checked by default Checkbox cbPython = new Checkbox("Python");
4. Radio Button (Using Checkbox + CheckboxGroup)
- Important Exam Fact: AWT does NOT have a separate
RadioButtonclass! Instead, Radio Buttons in AWT are created by groupingCheckboxobjects together inside aCheckboxGroupobject, so only ONE option can be selected at a time! - Example:
CheckboxGroup genderGroup = new CheckboxGroup(); Checkbox rbMale = new Checkbox("Male", genderGroup, true); Checkbox rbFemale = new Checkbox("Female", genderGroup, false);
5. Choice Menu (Drop-Down List / ComboBox)
Creates a compact pop-up / drop-down menu of choices from which the user can select a single item. Items are added using .add(String item).
- Example:
Choice cityChoice = new Choice(); cityChoice.add("Indore"); cityChoice.add("Bhopal"); cityChoice.add("Ujjain");
6. TextField and TextArea (Text Input Components)
Both inherit from TextComponent:
TextField: Allows editing a single line of text (e.g., Username or Password usingsetEchoChar('*')).TextField tfName = new TextField(20); // 20 columns wide
TextArea: Allows editing multiple lines of text (e.g., Address or Feedback box) with automatic scrollbars.TextArea taAddress = new TextArea("Enter Address...", 4, 30); // 4 rows, 30 cols
7. List (Scroll List)
Displays a scrollable list box of text items where multiple rows are visible simultaneously, and the user can select either a single item or multiple items.
- Constructor:
List(int visibleRows, boolean multipleMode) - Example:
List subjectList = new List(3, true); // Shows 3 rows, allows multiple selection subjectList.add("Java Programming"); subjectList.add("Cyber Security"); subjectList.add("Cloud Computing");
8. Scrollbar (Horizontal or Vertical Slider)
Used to select a numeric value from a continuous range (like a volume slider or RGB color mixer).
- Constructor:
Scrollbar(int orientation, int initialValue, int thumbSize, int min, int max) - Example:
Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 100);
5. Complete AWT Program Demonstrating Frame & All UI Components
import java.awt.*;
public class AWTComponentsDemo extends Frame {
public AWTComponentsDemo() {
// Set Frame Title, Size, and FlowLayout
setTitle("BCA Student Registration Form - AWT");
setSize(450, 400);
setLayout(new FlowLayout());
// 1. Label & TextField
add(new Label("Full Name:"));
add(new TextField(20));
// 2. Radio Buttons (CheckboxGroup)
add(new Label("Semester:"));
CheckboxGroup semGroup = new CheckboxGroup();
add(new Checkbox("Sem IV", semGroup, true));
add(new Checkbox("Sem VI", semGroup, false));
// 3. Checkboxes
add(new Label("Skills:"));
add(new Checkbox("Java", true));
add(new Checkbox("SQL"));
// 4. Choice Menu (Drop-down)
add(new Label("City:"));
Choice city = new Choice();
city.add("Indore");
city.add("Bhopal");
add(city);
// 5. TextArea (Multi-line text)
add(new Label("Address:"));
add(new TextArea(3, 25));
// 6. Scroll List
List courses = new List(2, false);
courses.add("BCA Honours");
courses.add("MCA Integrated");
add(courses);
// 7. Scrollbar
add(new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 100));
// 8. Button
add(new Button("Register Student"));
setVisible(true);
}
public static void main(String[] args) {
new AWTComponentsDemo();
}
}
💡 Exam Preparation Tips for DAVV BCA
- Trick Question Alert: If asked “How do you create a Radio Button in AWT?”, clearly state that AWT has no
RadioButtonclass; you pass aCheckboxGroupobject into theCheckboxconstructor! - Differentiate clearly between
Choice(drop-down menu showing 1 item at a time, single selection only) andList(scroll list showing multiple rows at once, supports multiple selection).