Docsity

Event-Driven Programming: An Introduction to Handling Events in Java and C#, Exercises of Programming Languages

Partial preview of the text

Download Event-Driven Programming: An Introduction to Handling Events in Java and C# and more Exercises Programming Languages in PDF only on Docsity!

Concepts of Programming Languages

Lecture 20 - Event-Driven Programming

Patrick Donnelly

Montana State University

Spring 2014

Administrivia

Assignments:

Programming #4 : due 04.

Reading:

Chapter 14

Event-Driven Programming

A conventional model of computation has the program prescribe the exact order of input.

Programs terminate once the input is exhausted.

Event-driven programs do not control the sequence in which input events occur.

Event Handling

Definition

An event is a notification that something specific has occurred, such as a mouse click on a graphical button.

Definition

The event handler is a segment of code that is executed in response to an event.

Imperative and Event-Driven Paradigms Contrasted

Event Sources

Input to an event-driven program comes from autonomous event sources.

Events occur asynchronously.

Example: human, robot sensors, engine sensors

Model-View-Controller (MVC)

Model: the object being implemented. Ex: game, calculator.

Controller: input mechanisms. Ex: buttons, menus, combo boxes.

View: output.

Ex: Tic-Tac-Toe Model

Whose turn is it?

State of the board.

Has someone won?

Are there no empty squares?

Java Swing GUI Components

Text box is an object of class JTextField

Radio button is an object of class JRadioButton

Applet’s display is a frame, a multilayered structure

Content pane is one layer, where applets put output

GUI components can be placed in a frame

Layout manager objects are used to control the placement of components

The Java Event Model

Definition

User interactions with GUI components create events that can be caught by event handlers, called event listeners.

An event generator tells a listener of an event by sending a message

An interface is used to make event-handling methods conform to a standard protocol

A class that implements a listener must implement an interface for the listener

Java Class AWTEvent and Its Subclasses

The Java Event Model

One class of events is ItemEvent, which is associated with the event of clicking a checkbox, a radio button, or a list item

The ItemListener interface prescribes a method, itemStateChanged, which is a handler for ItemEvent events

The listener is created with addItemListener

Components and their Event Handlers

Widget Listener Interface JButton ActionListener actionPerformed(ActionEvent e)

JComboBox ActionListener actionPerformed(ActionEvent e)

JLabel MouseListener mouseClicked(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e) MouseMotionListener mouseDragged(MouseEvent e) mouseMoved(MouseEvent e)

JTextArea ActionListener actionPerformed(ActionEvent e)

JTextField ActionListener actionPerformed(ActionEvent e)