Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

A lecture note from Montana State University's Spring 2014 'Concepts of Programming Languages' course, focusing on event-driven programming. The note covers the concept of event-driven programming, event handling, event sources, event properties, and provides examples using Java Swing and C#. Students will learn about event sources, event properties, and how to handle events using Java Swing and C#.

What you will learn

  • What are event sources in programming?
  • What are the properties of event-driven programs?
  • How do you handle events in C#?
  • What is event-driven programming?
  • How does event handling work in Java?

Typology: Exercises

2021/2022

Uploaded on 09/27/2022

lilylily
lilylily 🇬🇧

4

(8)

218 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Concepts of Programming Languages
Lecture 20 - Event-Driven Programming
Patrick Donnelly
Montana State University
Spring 2014
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 1 / 37
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

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)