Unit 5: GUI Programming using Java
Applet
5.1 Introduction to Applet
Definition
An Applet is a small Java program that runs inside a web browser or an applet viewer.
Unlike a standalone Java application, an applet does not have a main() method — its
execution starts automatically by the browser or applet viewer.
Key Points
● Applets are part of Java GUI programming (Graphical User Interface).
● They are mainly used for interactive web applications like animations, games,
calculators, etc.
● They run in a sandbox environment for security reasons — meaning limited access to
local system resources.
Applet Class Hierarchy
[Link]
↳ [Link]
↳ [Link]
↳ [Link]
↳ [Link]
Important Packages
● [Link].* — contains Applet class.
● [Link].* — contains GUI components (buttons, labels, etc.).
5.2 Difference between Applet and Application
Feature Applet Application
Definition Small Java program that runs in a Standalone Java program that runs
browser. independently.
Main Does not use main(); starts with Always begins with public static
Method init() and start(). void main().
Execution Runs in browser or applet viewer. Runs from command line or IDE.
User Created using AWT or Swing. Created using AWT, Swing, or
Interface console.
Security Runs in sandbox (restricted). Full access to local resources.
Output Displayed on browser window. Displayed on console or GUI window.
5.3 Life Cycle of an Applet
Each applet passes through five stages of its life cycle, managed by the browser or applet
viewer.
Applet Life Cycle Methods
Method Purpose
init() Called once when the applet is first loaded; used for initialization
(like constructors).
start() Called after init(), or when the applet is restarted (e.g., when
revisiting the page).
paint(Graphics g) Called automatically whenever the applet needs to redraw its
output.
stop() Called when the applet is stopped (e.g., switching to another
page).
destroy() Called before the applet is destroyed — used to release
resources.
Applet Life Cycle Diagram
+-----------+
| init() |
+-----------+
|
v
+-----------+
| start() |
+-----------+
|
v
+-----------+
| paint() |
+-----------+
|
+-----------+
| stop() |
+-----------+
|
v
+-----------+
| destroy() |
+-----------+
Example: Simple Applet
import [Link];
import [Link].*;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello, Welcome to Applet Programming!", 50, 50);
}
}
HTML File to Run Applet:
<applet code="[Link]" width="300" height="200">
</applet>
5.4 Invoking Applet and Passing Parameters
Invoking an Applet
Applets are invoked using an <applet> tag in HTML or using the applet viewer.
Example (via HTML):
<applet code="[Link]" width="300" height="150">
</applet>
Example (via Applet Viewer):
appletviewer [Link]
Passing Parameters to Applet
We can pass parameters through HTML using the <param> tag.
Example:
<applet code="[Link]" width="300" height="200">
<param name="studentName" value="Nisha">
<param name="subject" value="Java Programming">
</applet>
Java Code:
import [Link];
import [Link].*;
public class ParameterApplet extends Applet {
String name, subject;
public void init() {
name = getParameter("studentName");
subject = getParameter("subject");
}
public void paint(Graphics g) {
[Link]("Student: " + name, 50, 60);
[Link]("Subject: " + subject, 50, 80);
}
}
5.5 Abstract Window Toolkit (AWT)
Definition
AWT is a set of classes provided by Java for creating Graphical User Interfaces (GUI).
It includes classes for windows, buttons, menus, text fields, etc.
Hierarchy
Component → Container → Panel → Applet/Frame/Dialog
Important Classes
Class Description
Component Base class for all GUI components (buttons, labels, text fields).
Container Holds and manages components (like a panel or frame).
Panel A generic container inside a window or applet.
LayoutManager Determines how components are arranged in a container.
Common Layout Managers
Layout Manager Description
FlowLayout Places components in a row (default for Panel).
BorderLayout Divides the container into 5 regions: North, South, East, West, Center.
GridLayout Arranges components in rows and columns.
CardLayout Displays one component at a time.
5.6 UI Controls
Control Class Description Example Code
Label Label Displays non-editable Label l = new
text. Label("Name:");
TextField TextField Single-line text input. TextField t = new
TextField(20);
TextArea TextArea Multi-line text area. TextArea ta = new
TextArea(5, 30);
Button Button Creates a clickable Button b = new
button. Button("Submit");
Checkbox Checkbox Represents an on/off Checkbox c1 = new
option. Checkbox("Music");
RadioButto CheckboxGr Group of checkboxes for Checkbox c1 = new
n oup single selection. Checkbox("Male", g,
true);
Choice Choice Drop-down list of options. Choice ch = new
Choice();
[Link]("Java");
List List Scrollable list of multiple List lst = new List(3);
options. [Link]("Python");
5.6 Event Handling
What is Event Handling?
Event handling is the mechanism that controls the interaction between the user and GUI
components.
When a user performs an action (like clicking a button or typing text), an event is generated.
Event Handling Model
Java uses the Delegation Event Model:
1. Event Source → Component that generates the event (e.g., Button).
2. Event Object → Encapsulates information about the event.
3. Event Listener → Object that receives and handles the event.
Common Listener Interfaces
Interface Used For
ActionListener Buttons, Menu items
ItemListener Checkboxes, Radio buttons
TextListener TextField, TextArea changes
AdjustmentListener Scrollbars
KeyListener Keyboard events
MouseListener Mouse events
Syntax for Event Handling
[Link](new ListenerInterface() {
public void eventMethod(EventObject e) {
// Code to handle event
}
});
or (class implements listener):
public class MyApplet extends Applet implements ActionListener {
Button b1;
public void init() {
b1 = new Button("Click");
add(b1);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
showStatus("Button Clicked!");
}
}
5.6.1 Handling Button, Checkbox, RadioButton Events
Example: Button Click
public void actionPerformed(ActionEvent e) {
showStatus("Button Clicked!");
}
Example: Checkbox Selection
public void itemStateChanged(ItemEvent e) {
showStatus("Music Selected: " + [Link]());
}
Example: Radio Button
CheckboxGroup gender = new CheckboxGroup();
Checkbox male = new Checkbox("Male", gender, true);
Checkbox female = new Checkbox("Female", gender, false);
5.6.2 Handling ComboBox (Choice), List, TextField,
TextArea Events
Choice (ComboBox)
Choice ch = new Choice();
[Link]("Java");
[Link]("Python");
add(ch);
List
List lst = new List(3);
[Link]("C");
[Link]("C++");
[Link]("Java");
add(lst);
TextField
TextField tf = new TextField(20);
[Link](this);
public void actionPerformed(ActionEvent e) {
showStatus("Text Entered: " + [Link]());
}
TextArea
TextArea ta = new TextArea(5, 30);
add(ta);
✅ Summary
Concept Key Points
Applet Small Java program that runs in browser.
Life Cycle init(), start(), paint(), stop(),
destroy().
AWT Toolkit for GUI components.
UI Controls Label, Button, Checkbox, Radio, List, etc.
Event Handling ActionListener, ItemListener, etc.