20 Eventhandling
20 Eventhandling
Patrick Donnelly
Spring 2014
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 1 / 37
Administrivia
Assignments:
Reading:
Chapter 14
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 2 / 37
Of all men’s miseries the bitterest is this, to know so much
and to have control over nothing.
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 3 / 37
Event-Driven Programming
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 4 / 37
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.
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 5 / 37
Examples
Embedded applications:
cell phones
car engines
airplanes
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 6 / 37
Imperative and Event-Driven Paradigms Contrasted
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 7 / 37
Event Sources
Example:
human,
robot sensors,
engine sensors
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 8 / 37
Event Properties
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 9 / 37
Model-View-Controller (MVC)
Model: the object being implemented. Ex: game, calculator.
View: output.
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 10 / 37
Ex: Tic-Tac-Toe Model
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 11 / 37
Java GUI Application
Definition
A GUI application is a program that runs in its own window and
communicates with users using buttons, menus, mouse clicks, etc.
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 12 / 37
Java Swing GUI Components
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 13 / 37
The Java Event Model
Definition
User interactions with GUI components create events that can be
caught by event handlers, called event listeners.
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 14 / 37
Events in Java
Subclasses of AWTEvent
Example
for a JButton b:
b.addActionListener(listener)
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 15 / 37
Java Class AWTEvent and Its Subclasses
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 16 / 37
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 17 / 37
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 18 / 37
The Java Event Model
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 19 / 37
Components and their Event Handlers
import javax.swing.JFrame;
public class GUIApp {
public static void main (String[ ] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
MyApp app = new MyApp( ); // JPanel
frame.getContentPane().add(app);
frame.show( );
}
}
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 21 / 37
GUI Example using MVC
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 22 / 37
GUI Design
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 23 / 37
Instance Variables
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 24 / 37
Initialization Code (1/2)
public Skeleton( ) {
// Set the background color
// and mouse listener
setBackground(Color.white);
addMouseListener(new MouseHandler());
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 25 / 37
Initialization Code (2/2)
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 26 / 37
Action Listener
private class ComboHandler
implements ActionListener {
public void actionPerformed (ActionEvent e) {
String c = (String)
(combo.getSelectedItem());
echoArea.setText("Combo selected: " + c);
clickNumber = 0;
if (c.equals("Rectangle"))
echoArea.append("\nClick to set upper "
+ " left corner of the rectangle");
else if (c.equals("Message"))
echoArea.append(
"\nEnter message in the text area");
}
}
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 27 / 37
The User Selects Rectangle from the Menu
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 28 / 37
mouseClicked Handler (1/2)
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 29 / 37
mouseClicked Handler (2/2)
// is it the first click?
if (clickNumber % 2 == 1) {
echoArea.append("Click to set lower right"
+ " corner of the rectangle");
lastX = x;
lastY = y;
}
// or the second?
else g.drawRect(lastX, lastY,
Math.abs(x-lastX), Math.abs(y-lastY));
}
else if (combo.getSelectedItem().equals(
"Message"))
// for a message, display it
g.drawString(typing.getText(), x, y);
} // mouseClicked
}
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 30 / 37
Selecting Rectangle Choice and ClickingTwice
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 31 / 37
main method
frame.getContentPane().add(panel);
frame.setSize(500, 500);
frame.show();
}
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 32 / 37
Event Handling in C#
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 33 / 37
Event Handling in C#
All C# event handlers have the same protocol, the return type is void and the
two parameters are of types object and EventArgs
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 34 / 37
Event Handling in C#
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 35 / 37
Event Handling in C#
plain.CheckedChanged +=
new EventHandler (rb_CheckedChanged);
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 36 / 37
Summary
Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 37 / 37