Lecture 20 Event Handling
Lecture 20 Event Handling
Event Handling
Delegation Event Model
Concept
:• A source generates an event and sends it to one or more
listeners.
• Listener simply waits until it receives an event.
• Once an event is received, the listener processes the event and
returns.
• Listener must register with a source in order to receive an event
notification.
Event: An event is an object that describes change in state in a
source.
As a result of person interacting with GUI: pressing button, clicking
mouse, entering character from KB
w/o person interaction : timer expires, counter exceeds some value
Event Listeners
When the user performs an action, Java creates an
object containing information about the event.
Responding to an event is done by writing a method
that can be called when the event occurs.
Steps involved in handling an event:
1. The user performs an action, causing an event to be triggered
(or fired).
2. An object is created that contains information about the event,
including an indication of which component was involved.
3. A method that belongs to a listener object is called. The object
created in step 2 is passed to the method.
Events
When an event occurs, an object is created that
contains information about the event.
This object will belong to one of several different
classes, depending on the nature of the event.
All these classes belong to the
java.awt.event package.
Java divides events into two groups: “high-level”
events and “low-level” events.
Events
High-level Events:
Class Name Description of Event
ActionEvent A significant action has been performed on
a component (a button was pressed, a list
item was double-clicked, or the Enter key
was pressed in a text field).
// Driver class
public class ButtonTest2
{
public static void main(String[] args)
{
Frame f = new ButtonTestFrame("Button Test");
f.setSize(150, 100);
f.setVisible(true);
}
}
// Frame class
class ButtonTestFrame extends Frame {
public ButtonTestFrame(String title) {
super(title);
setLayout(new FlowLayout());
Button b = new Button("Close window");
add(b);
b.addActionListener(new ButtonListener());
}
}
import java.awt.*;
import java.awt.event.*;
// Driver class
public class ButtonTest3 {
public static void main(String[] args) {
Frame f = new ButtonTestFrame("Button Test");
f.setSize(150, 100);
f.setVisible(true);
}
}
// Frame class
class ButtonTestFrame extends Frame {
public ButtonTestFrame(String title) {
super(title);
setLayout(new FlowLayout());
Button b = new Button("Close window");
add(b);
b.addActionListener(new ButtonListener());
import java.awt.*;
import java.awt.event.*;
// Driver class
public class ChangeColor {
public static void main(String[] args) {
Frame f = new ChangeColorFrame("Change
Color");
f.setSize(160, 100);
f.setVisible(true);
}
}
// Frame class
class ChangeColorFrame extends Frame {
public ChangeColorFrame(String title) {
// Set title, layout, and background color
super(title);
setLayout(new FlowLayout());
setBackground(Color.white);
// Driver class
public class ChangeColor2 {
public static void main(String[] args) {
Frame f = new ChangeColorFrame("Change Color");
f.setSize(160, 100);
f.setVisible(true);
}
}
// Frame class
class ChangeColorFrame extends Frame {
public ChangeColorFrame(String title) {
// Set title, layout, and background color
super(title);
setLayout(new FlowLayout());
setBackground(Color.gray);
// Create button listener
ButtonListener listener = new ButtonListener();
import java.awt.*;
import java.awt.event.*;
import jpb.*;
// Driver class
public class ConvertTemp {
public static void main(String[] args) {
Frame frame =
new ConvertTempFrame("Temperature Conversion");
frame.setSize(150, 75);
frame.setVisible(true);
}
}
// Frame class
class ConvertTempFrame extends Frame {
private TextField fahrenField = new TextField();
private TextField celsiusField = new TextField();
// Constructor
public ConvertTempFrame(String title) {
// Set title for frame and choose layout
super(title);
setLayout(new GridLayout(2, 2));
g
ShowDefinition.java
// Shows the definition of a term
import java.awt.*;
import java.awt.event.*;
// Driver class
public class ShowDefinition {
public static void main(String[] args) {
Frame f = new ShowDefinitionFrame("Show
Definition");
f.setSize(300, 160);
f.setVisible(true);
}
}
// Frame class
class ShowDefinitionFrame extends Frame
{ private List termList = new List();
private TextArea definitionArea = new
private TextArea();
String[]
{"Button", terms"Choice",
"Checkbox", = "Label",
"List", "Scrollbar", "TextArea",
"TextField"};
private String[]
{"A labeled definitions
button that can\nbe= pressed",
"A box that can be clicked\n\"on\"
"A or \"off\"",
"A menu that displays one\nitem at a time",
string that can be\npositioned next to " +
"other\ncomponents",
"A scrolling list of items",
"A sliding bar that can be\neither horizontal " +
or
"A "vertical",
multiline area in which\ntext can be displayed " +
"or\nedited",
"A single line of text that\ncan be displayed " +
"or\nedited"};
// Constructor
public ShowDefinitionFrame(String title)
{
// Set title for frame
super(title);
// Put terms in term list; add term list to frame
for (int i = 0; i < terms.length; i++)
termList.add(terms[i]);
termList.addItemListener(new
ListListener());
add("West", termList);
// Make definition area not editable and add to frame
definitionArea.setEditable(false);
add("Center", definitionArea);
import java.awt.*;
import java.awt.event.*;
// Driver class
public class PickColor {
public static void main(String[] args) {
Frame f = new PickColorFrame("Pick Color");
f.setSize(150, 200);
f.setVisible(true);
}
}
// Frame class
class PickColorFrame extends Frame
{private Label redLabel = new Label("Red = 128", Label.CENTER);
private Label greenLabel = new Label("Green = 128", Label.CENTER);
private Label blueLabel = new Label("Blue = 128", Label.CENTER);
private Scrollbar redBar =
new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
private Scrollbar greenBar =
new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
private Scrollbar blueBar =
new 128, 1, 0, 256);
Scrollbar(Scrollbar.HORIZONTAL,
// Constructor
public PickColorFrame(String title) {
// Set title, background color, and
layout
super(title);
setBackground(new Color(128, 128, 128));
setLayout(new GridLayout(6, 1));
PickColor.java
// Create scrollbar listener
ScrollbarListener listener = new ScrollbarListener();