- Forward


Event-Driven Programs
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Multi-Threaded Programming:
    • It can be very difficult to ensure that the right things happen at the right time
  • Overcoming this Difficulty:
    • Focuses on how classes should handle events rather than on how or when these events will occur
Event-Driven Design
Back SMYC Forward
  • Focus On:
    • The events that can occur (e.g., mouse clicks, timing signals, key presses)
    • The classes that can generate events of different kinds (often called event generators)
    • The classes that need to respond to events of different kinds (often called event receivers)
  • Do Not Focus On:
    • The order in which events occur
Event-Driven Design (cont.)
Back SMYC Forward

Using an Event Queue

event-queue
Roles of the Event Queue
Back SMYC Forward
  • Central repository for all events
  • Ensures that events are processed in the right order
  • Decouples the generators and the receivers
Variants
Back SMYC Forward

Event Listening

event-listening

Event Bubbling

event-bubbling
The Event Queue and Dispatch Thread in Java
Back SMYC Forward
  • EventQueue :
    • Fires events using dispatchEvent()
    • Can coalesce events that are posted to it
  • Event Dispatch Thread (EDT):
    • All events are dispatched in this thread
    • Tasks that execute in this thread should "finish quickly"
    • Code can be executed in the event dispatch thread using SwingUtilities.invokedAndWait() and SwingUtilities.invokeLater()
GUIs in Java
Back SMYC Forward
  • Why Bother?
    • It is impossible to build interesting event-based applications in Java without using GUI "widgets"
  • Our Objective Here:
    • Consider only what we need
  • "Widgets" in Swing:
    • Components
    • Containers
Components in Swing
Back SMYC Forward
  • JLabel :
    • Displays a String or Icon or both
  • JButton :
    • One of the simplest components that allows for user interaction
    • Contains a String or Icon or both
    • Can be "clicked"
Components in Swing (cont.)
Back SMYC Forward

Using a JLabel

javaexamples/app/BadInteractiveRandomMessageSwingApplication.java (Fragment: label0)
 
Components in Swing (cont.)
Back SMYC Forward

Using a JButton

javaexamples/app/BadInteractiveRandomMessageSwingApplication.java (Fragment: button0)
 
Containers in Swing
Back SMYC Forward
  • Top-Level:
    • Simplest example -- JFrame
    • Should not be used as a container; should use the root pane
  • Ordinary:
    • Simplest example -- JPanel
Containers in Swing (cont.)
Back SMYC Forward

Using a JFrame

javaexamples/app/BadInteractiveRandomMessageSwingApplication.java (Fragment: jframe)
 
Containers in Swing (cont.)
Back SMYC Forward

Getting the Content Pane

javaexamples/app/BadInteractiveRandomMessageSwingApplication.java (Fragment: contentPane0)
 
Layout in Swing
Back SMYC Forward
  • Defined:
    • The process of positioning and sizing the components in a container
  • The "Normal" Approach:
    • Use a LayoutManager
  • A Simpler Approach:
    • Use absolute/null layout (i.e., explicitly set the position and size)
    • [Note: (0,0) is at the upper left]
Layout in Swing (cont.)
Back SMYC Forward

Using Absolute Layout

javaexamples/app/BadInteractiveRandomMessageSwingApplication.java (Fragment: contentPane1)
 
javaexamples/app/BadInteractiveRandomMessageSwingApplication.java (Fragment: label1)
 
javaexamples/app/BadInteractiveRandomMessageSwingApplication.java (Fragment: button1)
 
Java Programs
Back SMYC Forward
  • Defined Loosely:
    • A group of cooperating objects with a well-defined entry point (i.e., a method that should be executed first) and, perhaps, an exit point
  • Types:
    • Desktops -- applications and applets
    • Servers -- servlets
    • Mobile Devices -- midlets
Java Applications
Back SMYC Forward
  • Environment:
    • Operating system
  • Top-Level Container:
    • JFrame
  • Entry Point:
    • main()
Java Applets
Back SMYC Forward
  • Environment:
    • Browser (or other application)
  • Top-Level Container:
    • JApplet
  • Entry Point:
    • init() then start()
Simple GUI Applications
Back SMYC Forward

With a Problem

javaexamples/app/BadRandomMessageApplication.java
 
Simple GUI Applications (cont.)
Back SMYC Forward

Using the Event Dispatch Thread

javaexamples/app/BadRandomMessageSwingApplication.java
 
Simple GUI Applications (cont.)
Back SMYC Forward

Adding Event Handling

javaexamples/app/BadInteractiveRandomMessageSwingApplication.java
 
Simple GUI Applets
Back SMYC Forward

Like the Above Applications

javaexamples/app/BadRandomMessageJApplet.java
 
javaexamples/app/BadInteractiveRandomMessageJApplet.java
 
Unifying Applications and Applets
Back SMYC Forward
  • Observations:
    • The general structure of applets and applications are very similar
    • The details are different enough that there is no easy way to create an applet and an application with the same functionality without copying and pasting code from one method in one class to another method in another class
  • Towards Unification:
    • Create an abstract JApplication class that mimics the programming interface and life-cycle of the JApplet class
Unifying Applications and Applets (cont.)
Back SMYC Forward

The JApplication Class

javaexamples/app/JApplication.java (Fragment: run)
 
javaexamples/app/JApplication.java (Fragment: constructMainWindow2)
 
javaexamples/app/JApplication.java (Fragment: init)
 
Unifying Applications and Applets (cont.)
Back SMYC Forward

An Example JApplication

javaexamples/app/BadRandomMessageJApplication.java
 
Unifying Applications and Applets (cont.)
Back SMYC Forward
  • A Technical Problem:
    • The "transition" methods are not called in the event dispatch thread
  • A Design-Related Problem:
    • If you write a JApplet and JApplication with the same functionality they will have an enormous amount of duplicate code
  • An Historical Problem:
    • The RootPaneContainer for a JApplet has access to the start-up parameters whereas the RootPaneContainer for a JApplication (which is a JFrame) does not.
Unifying Applications and Applets (cont.)
Back SMYC Forward

Correcting the Historical Problem

javaexamples/app/MultimediaRootPaneContainer.java
 
Unifying Applications and Applets (cont.)
Back SMYC Forward

Correcting the Remaining Problems with the Decorator Pattern

multimedia-app_initial
Unifying Applications and Applets (cont.)
Back SMYC Forward

The Interface

javaexamples/app/MultimediaApp.java
 
Unifying Applications and Applets (cont.)
Back SMYC Forward

The MultimediaApplet Class

javaexamples/app/MultimediaApplet.java
 
Unifying Applications and Applets (cont.)
Back SMYC Forward

An Example App

javaexamples/app/RandomMessageApp.java
 
Unifying Applications and Applets (cont.)
Back SMYC Forward

The Application and Applet

javaexamples/app/RandomMessageMultimediaApplet.java
 
javaexamples/app/RandomMessageMultimediaApplication.java
 
Unifying Applications and Applets (cont.)
Back SMYC Forward

Unifying Calls to the "Transition" Methods

javaexamples/app/JApplication.java (Fragment: constructMainWindow3)
 
javaexamples/app/JApplication.java (Fragment: windowOpened)
 
javaexamples/app/JApplication.java (Fragment: windowDeiconified)
 
javaexamples/app/JApplication.java (Fragment: windowIconified)
 
javaexamples/app/JApplication.java (Fragment: windowClosing)
 
javaexamples/app/JApplication.java (Fragment: exit)
 
javaexamples/app/JApplication.java (Fragment: windowClosed)
 
Unifying Applications and Applets (cont.)
Back SMYC Forward

The Final Design

multimedia-app
Unifying Applications and Applets (cont.)
Back SMYC Forward

Loading Resources

javaexamples/io/ResourceFinder.java
 
Timed Events
Back SMYC Forward
  • Events that occur at a particular point in time
  • Events that occur after a particular interval of time
  • Events that recur after a particular interval of time (called fixed-delay execution)
  • Events that recur at a particular rate (called fixed-rate execution)
Timed Events (cont.)
Back SMYC Forward

Design of a Simple Metronome

metronome
Timed Events (cont.)
Back SMYC Forward
javaexamples/event/MetronomeListener.java
 
Timed Events (cont.)
Back SMYC Forward
javaexamples/event/Metronome.java (Fragment: listeners)
 
javaexamples/event/Metronome.java (Fragment: notifyListeners)
 
javaexamples/event/Metronome.java (Fragment: MetronomeTickDispatcher)
 
javaexamples/event/Metronome.java (Fragment: start)
 
javaexamples/event/Metronome.java (Fragment: run)
 
Timed Events (cont.)
Back SMYC Forward

A Simple Example

javaexamples/app/StopWatchApp.java
 
There's Always More to Learn
Back -