Weeks 11 12 _GUI Development and Advanced Event Handling in Java
Weeks 11 12 _GUI Development and Advanced Event Handling in Java
Topic: Design Graphical User Interfaces (GUIs) using Swing and AWT
Learning Outcomes:
1. Understand the basics of Graphical User Interface (GUI) development using Swing and
AWT.
2. Create a simple Java GUI using common Swing components such as JFrame, JButton,
JTextField, and more.
In Java, Swing and AWT (Abstract Window Toolkit) are two popular libraries for creating
graphical user interfaces (GUIs). AWT is Java's original GUI toolkit, while Swing is an extension of
AWT that provides a richer set of components and greater flexibility for building user-friendly
applications.
Swing: A more modern toolkit that is built on top of AWT but provides more powerful and
customizable components. It is platform-independent and more commonly used today for
building GUIs.
Let's start by building a simple GUI that contains a window with a label, a text field, and a button.
When the user clicks the button, the text they enter into the text field will be displayed in the
console.
JButton: A button labeled "Submit" that, when clicked, triggers an event to read and print
the name from the JTextField.
ActionListener: Captures the button click event and executes the code in the
actionPerformed() method.
Task:
2. When the user enters text into the text field and clicks the button, display the text in the
console.
Extra Task:
Add a second label that dynamically updates to show what the user entered (instead of
printing it only in the console).
Week 12: Advanced GUI Layouts and Event Handling
Learning Outcomes:
3. Handle advanced event handling for multiple components, including buttons, text fields,
and combo boxes.
In Java, layout managers are used to arrange GUI components within a container. Instead of
manually positioning each component (like in Week 11), layout managers automatically arrange
components based on a specific layout style. Common layout managers include:
2. BorderLayout: Divides the container into five regions: North, South, East, West, and
Center.
Explanation:
FlowLayout arranges the buttons in a row. When the window is resized, the buttons will
adjust their position automatically based on the size of the window.
import javax.swing.*;
import java.awt.event.*;
// Create components
JLabel label = new JLabel("Choose an option:");
JComboBox<String> comboBox = new JComboBox<>(new String[]{"Option 1",
"Option 2", "Option 3"});
JButton button = new JButton("Click Me");
JComboBox: A drop-down list where the user can select one of several options.
ActionListener for JComboBox: Detects when the user selects an option from the combo
box and prints the selected option to the console.
ActionListener for JButton: Detects when the button is clicked and prints "Button
clicked!" to the console.
3. Using BorderLayout
The BorderLayout manager divides the container into five regions: North, South, East, West, and
Center. You can place components in these regions to organize them on the screen.
import javax.swing.*;
import java.awt.*;
Each button is placed in a different region of the frame (north, south, east, west, center).
Objective: In this exercise, you will create an advanced Java GUI that utilizes the BorderLayout
layout manager to organize components on the screen. You will also implement event handling
to interact with a combo box and a button. The exercise will help you understand how to manage
GUI components, layout managers, and event handling in a more structured way.
Instructions:
o Place a JComboBox in one of the regions (you can choose where it fits best).
o Populate the combo box with at least three options (e.g., “Option 1,” “Option 2,”
“Option 3”).
o When the user selects an item from the combo box, display the selected item in
the console.
o When the user clicks the button, display a message in the console, such as
“Button clicked!”.
Dynamically update the text in one of the labels based on the selection from the combo
box.
Add a JTextField to allow the user to enter text, and when the button is clicked, display
the entered text in the console.
Step-by-Step Breakdown:
o Add a label to the North region, prompting the user to interact with the GUI.
o Add a combo box to the West region, and populate it with options.
o Add a text field to the East region to allow the user to input data.
o Add another component of your choice (e.g., another label or button) to the
Center region.
o Add event handling for the combo box to display the selected item in the console
when the user selects an option.
o Add event handling for the button to print a message to the console when it is
clicked.