Lab04 Agenda
Lab04 Agenda
(12h30-1h15)
(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.*;
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.*;
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.
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
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