





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























Lecture 20 - Event-Driven Programming
Patrick Donnelly
Montana State University
Spring 2014
Assignments:
Programming #4 : due 04.
Reading:
Chapter 14
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.
An event is a notification that something specific has occurred, such as a mouse click on a graphical button.
The event handler is a segment of code that is executed in response to an event.
Input to an event-driven program comes from autonomous event sources.
Events occur asynchronously.
Example: human, robot sensors, engine sensors
Model: the object being implemented. Ex: game, calculator.
Controller: input mechanisms. Ex: buttons, menus, combo boxes.
View: output.
Whose turn is it?
State of the board.
Has someone won?
Are there no empty squares?
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
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
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
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)