0% found this document useful (0 votes)
10 views17 pages

Lab04 Agenda

This document outlines a lab focused on GUI programming with Java's Swing and AWT frameworks. It covers the creation of simple GUI applications, the conversion of a console application to a GUI, and introduces key concepts such as event handling and the Model-View-Controller architecture. Additionally, it provides example code snippets and links to further resources for learning AWT and Swing.

Uploaded by

thaiphucdat2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

Lab04 Agenda

This document outlines a lab focused on GUI programming with Java's Swing and AWT frameworks. It covers the creation of simple GUI applications, the conversion of a console application to a GUI, and introduces key concepts such as event handling and the Model-View-Controller architecture. Additionally, it provides example code snippets and links to further resources for learning AWT and Swing.

Uploaded by

thaiphucdat2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Lab04: GUI programming with Swing

In this lab, you will practice with:


● Create simple GUI applications with Swing
● Convert part of the Aims Project
from the console/command-line (CLI) application to the GUI one.
1st period ● Abstract Window Toolkit (AWT)

(12h30-1h15)

2nd period ● Java Swing

(1h15 - 2h00)
Java APIs for GUI programming
1. Abstract Window Toolkit
- Part of java.awt package
- Integrate into core Java since JDK 1.0
2. Swing
- Java Swing is the advanced and optimized version of AWT
- Part of javax.swing package
- Integrate into core Java since JDK 1.2
3. JavaFX
- The JavaFX library is written in Java and is available for the languages that can be executed on a JVM.
- JavaFX applications can run on multiple platforms: desktop, mobile and embedded systems built on Java.
Java conceptual diagram
Part 1: AWT
Part 2: Swing
Example 1: UI elements

import java.awt.*;

public class AWTExample {


public static void main (String[] args) {
Frame frame = new Frame("OOP class - Lab04");
frame.setSize(500, 300);

Label label = new Label("AWT Example");

frame.add(label);
frame.setVisible(true);

}
}
AWT hierarchy of elements
Docs:
https://docs.oracle.com/javase/7/docs/api/in
dex.html?java/awt/package-summary.html

https://docs.oracle.com/javase/8/docs/api/ja
va/awt/package-frame.html
Example 2: Layout
import java.awt.*;

public class AWT_Example2 {


public static void main(String[] args) {
Frame frame = new Frame("OOP class - Lab04");
Label label = new Label("AWT Example");
Button button = new Button("Submit");

frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(button);

frame.setSize(500, 300);
frame.setVisible(true);
}
}
AWT layouts
Example 3: Events
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AWT_Example3 {
public static void main(String[] args) {
Frame frame = new Frame("OOP class - Lab04");
Label label = new Label("AWT Example");
Button button = new Button("Submit");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");
}
});
frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(button);

frame.setSize(500, 300);
frame.setVisible(true);
}
}
AWT Event handling
Package java.awt.event: contains AWT
event-handling classes
3 objects involved in the event-handling:
source, listener, event
– source object interacts with the user to
create an event object
– event object will be messaged to all the
registered listener objects
– appropriate event-handler method of the
listener(s) is called-back to provide the
response
– subscribe - publish pattern: The listener(s) "subscribes" to an event of a source, and the source "publishes" the
event to all its subscribers upon activation
Event Listeners As Observers
Using event listeners to handle an event is a
three-step process:
class AnActionListener implements ActionListener {
1. Defining the Listener: Define a class that public void actionPerformed(ActionEvent e) {
implements the appropriate listener System.out.println("I was selected.");
}
interface (this includes providing }
implementations for all the methods of the
interface).
ActionListener actionListener = new AnActionListener();
2. Create an instance of this listener.

3. Register this listener to the component


whose events you’re interested in. button.addActionListener(actionListener);
Part 1: AWT
Part 2: Swing
Introduction to Java Swing
As Java technologies became more popular, users realized AWT was extremely slow and unreliable, and
you couldn’t really do much with the provided components…
With these new technologies came more and more widget sets, for the AWT component set itself was
very basic. So, applet download times grew and grew, because these new widget sets weren’t part of the
core Java platform, and Java archive (JAR) files were introduced to improve delivery time. Eventually,
each of the major browser vendors added its favorite component library to its virtual machine—AFC, IFC,
and WFC, to name just a few. Yet all the libraries used different design models, and there were no true
cross-browser standards.
Eventually, Sun Microsystems teamed up with Netscape Communication and other partners to create yet
another library called the Java Foundation Classes, or JFC. Part of JFC is something called the Swing
component set.
- The Definitive Guide to Java Swing, John Zukowski
Swing hierarchy of elements

AWT and Swing elements


The Model-View-Controller architecture
The model part of the MVC holds the state of a component and serves as
the Subject.

The view part of the MVC serves as the Observer of the Subject to
display the model’s state.

The view creates the controller, which defines how the user interface
reacts to user input

This architecture enabling modular development of each part


independently, enhancing maintainability and scalability.

Model-View-Controller (MVC) design pattern is widely used in Java


applications for web development and user interface separation.

Example: https://github.com/ashiishme/java-swing-mvc
Videos
AWT:
https://youtube.com/playlist?list=PLCRogJ_v4BQUuRtS3t_s3TpGFHk8Rsraz&feature=sh
ared

Swing:

https://youtube.com/playlist?list=PLjJmj2FyqToaShAsuiW57BFvAkgMpaaNk&feature=s
hared

You might also like