Week 8 Lab Tutorial (Step-by-Step)
Course: BIC1224 – Object-Oriented Programming (Java)
Topic: Event-Driven Programming (Swing) — ActionListener, Button Click Events, Input
Validation, JOptionPane
Learning Outcomes (Week 8 Lab)
By the end of this lab, students will be able to:
● Attach ActionListener to Swing components
● Handle button click events using actionPerformed() or lambda
● Read input from JTextField / JPasswordField
● Validate user input (empty, numeric, range)
● Display messages using JOptionPane
● Build small interactive GUI apps (login, calculator, converter)
Part A — Setup & Project Structure
Step 1: Create a New Project
1. Open your IDE.
2. Create a Java project:
○ Project Name: BIC1224_Week8_Events
3. Create a class:
○ [Link]
Step 2: Import Required Packages
import [Link].*;
import [Link].*;
import [Link].*;
Part B — First Event: Button Click
Task 1: Create a Button and Show a Message
Step 3: Build a basic GUI
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Week 8 - Events");
[Link](400, 250);
[Link](JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JButton btn = new JButton("Click Me");
[Link](btn);
[Link](panel);
[Link](true);
}
}
Step 4: Attach ActionListener to Button
Add this before [Link](true);:
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](frame, "Button clicked!");
}
});
✅ Now clicking the button shows a popup message.
Part C — Read Input from TextField
Task 2: Read Name and Display Greeting
Step 5: Add label, textfield, button
JLabel lbl = new JLabel("Enter Name:");
JTextField tf = new JTextField(15);
JButton btnGreet = new JButton("Greet");
[Link](lbl);
[Link](tf);
[Link](btnGreet);
Step 6: Handle button click and read input
[Link](e -> {
String name = [Link]();
if ([Link]().isEmpty()) {
[Link](frame, "Name cannot be empty!");
} else {
[Link](frame, "Hello, " + name + "!");
}
});
✅ You practiced: reading input + validation + dialog output.
Part D — Login Form with Validation
Task 3: Create Login UI and Validate Credentials
Step 7: Create login panel using GridLayout
Replace panel layout:
[Link](new GridLayout(3, 2, 10, 10));
[Link]([Link](20, 20, 20, 20));
JLabel lUser = new JLabel("Username:");
JTextField tfUser = new JTextField();
JLabel lPass = new JLabel("Password:");
JPasswordField pfPass = new JPasswordField();
JButton btnLogin = new JButton("Login");
JButton btnClear = new JButton("Clear");
[Link](lUser); [Link](tfUser);
[Link](lPass); [Link](pfPass);
[Link](btnLogin); [Link](btnClear);
Step 8: Login button action
[Link](e -> {
String user = [Link]().trim();
String pass = new String([Link]());
if ([Link]() || [Link]()) {
[Link](frame, "Username and Password required!");
return;
}
if ([Link]("admin") && [Link]("1234")) {
[Link](frame, "Login Successful!");
} else {
[Link](frame, "Invalid Credentials!");
}
});
Step 9: Clear button action
[Link](e -> {
[Link]("");
[Link]("");
[Link]();
});
✅ You practiced: JPasswordField, validation, multiple listeners.
Part E — Numeric Input Validation (Very
Important)
Task 4: Safe Integer Parsing (Try-Catch)
Step 10: Create a small “Age Checker”
Create UI:
● TextField for age
● Button “Check”
● Output message box
Validation logic:
[Link](e -> {
String sAge = [Link]().trim();
if ([Link]()) {
[Link](frame, "Age is required!");
return;
}
try {
int age = [Link](sAge);
if (age < 0) [Link](frame, "Age cannot be negative!");
else if (age < 18) [Link](frame, "Minor");
else [Link](frame, "Adult");
} catch (NumberFormatException ex) {
[Link](frame, "Age must be a number!");
}
});
✅ You practiced: try-catch + numeric validation
Part F — Mini GUI App: Calculator
(Events)
Task 5: Two-number Calculator
Step 11: Create UI
Use 3 textfields:
● number1
● number2
● result (non-editable)
Buttons:
● Add, Sub, Mul, Div
Step 12: Add action for Add button
[Link](e -> {
try {
double a = [Link]([Link]().trim());
double b = [Link]([Link]().trim());
[Link]([Link](a + b));
} catch (NumberFormatException ex) {
[Link](frame, "Please enter valid numbers!");
}
});
Step 13: Division safety
[Link](e -> {
try {
double a = [Link]([Link]().trim());
double b = [Link]([Link]().trim());
if (b == 0) {
[Link](frame, "Cannot divide by zero!");
return;
}
[Link]([Link](a / b));
} catch (NumberFormatException ex) {
[Link](frame, "Please enter valid numbers!");
}
});
✅ You practiced: arithmetic events + error control.
Exercises (Week 8)
Exercise 1 — Temperature Converter GUI
UI:
● Input field for Celsius
● Buttons:
○ Convert to Fahrenheit
○ Convert to Kelvin
● Output displayed in label/textfield
Include validation + NumberFormatException handling.
Exercise 2 — Counter Application
UI:
● Label displays counter value
Buttons:
● Increment
● Decrement
● Reset
Rules:
● Value cannot go below 0 (validation required)
Exercise 3 — Student CGPA Validator
Input:
● Name
● CGPA
Button: Validate
Rules:
● CGPA must be between 0.0 and 4.0
Output: JOptionPane message
Exercise 4 — Simple Quiz App
UI:
● One question
● Options using JRadioButtons
● Button: Submit
Output: Score message
Exercise 5 (Challenge) — Multi-Page GUI Navigation
Login window + Dashboard window:
● If login success → open dashboard and close login
Dashboard buttons (no advanced logic needed):
● “Student Form”
● “Calculator”
● “Exit”
(Use events to open new frames.)