CS3135/CS2135
Object Oriented Programming (Java)
BSCS-3 / MCS-3
Lecture # 11
Event Handling
Part-1
Handling Events
• Interaction of user with GUI components is called event.
• Java events are part of the Java AWT package.
• Source of an event is the component that causes that event to occur.
• Listener of an event is an object that receives the event and processes it appropriately.
• The code that performs a task in response to an event is called an event handler.
• Overall process of responding to events is known as event handling.
• GUIs are event driven.
Event Handling Process
• When an event is triggered, the JAVA runtime first determines its source and type.
• If a listener for this type of event is registered with the source, an event object is created.
• For each listener to this type of an event, the JAVA runtime invokes the appropriate event handling
method to the listener and passes the event object as the parameter.
Sources of Events
• Button, Checkbox, Choice, List, Menu Item, Scrollbar, Text Components, Window, etc
Event Handling
Instructor: Tanzila Kehkashan
2
Event Listener Interfaces and their Methods Event classes and their methods
ActionListener
• void actionPerformed (ActionEvent ae)
ActionEvent
Generated when a button is pressed, a list is double-clicked, or a menu
item is selected.
• String getActionCommand()
returns string containing event source caption
• Object getSource()
ComponentListener
• void componentResized (ComponentEvent ce)
• void componentMoved (ComponentEvent ce)
• void componentShown (ComponentEvent ce)
• void componentHidden (ComponentEvent ce)
ComponentEvent
Generated when a component is hidden, moved, resized, or becomes
visible.
• String getComponent().getClass().getName()
• Object getSource()
FocusListener
• void focusGained (FocusEvent fe)
• void focusLost (FocusEvent fe)
FocusEvent
Generated when a component gains or loses keyboard focus.
• String getComponent().getClass().getName()
• Object getSource()
• An interface is a group of related methods with empty bodies.
• Event classes and their listener interfaces are available in java.awt.event
Event Handling
Instructor: Tanzila Kehkashan
3
Instructor: Tanzila Kehkashan
4
ASSIGNMENT
Q#1: Write a program which has two buttons. After each click, the program
determines how many times that specific button has been clicked.
Q#2: Make online admission form of student in three steps.
• In 1st frame, get personal bio-data of student,
• In 2nd frame, get academic record of student, and
• In 3rd frame, display all the information of student you collected in previous two
frames.
Instructor: Tanzila Kehkashan
5
ASSIGNMENT
Write a program which consists of two drop down Lists and four buttons having
captions “>”, “>>”, “<”, “<<”. When user clicks on button “>”, selected item from
left list moves towards right list. Rest of three buttons work accordingly.
Instructor: Tanzila Kehkashan
6
Instructor: Tanzila Kehkashan
7

OOP Lecture 11-EventHandling1.pptx

  • 1.
    CS3135/CS2135 Object Oriented Programming(Java) BSCS-3 / MCS-3 Lecture # 11 Event Handling Part-1
  • 2.
    Handling Events • Interactionof user with GUI components is called event. • Java events are part of the Java AWT package. • Source of an event is the component that causes that event to occur. • Listener of an event is an object that receives the event and processes it appropriately. • The code that performs a task in response to an event is called an event handler. • Overall process of responding to events is known as event handling. • GUIs are event driven. Event Handling Process • When an event is triggered, the JAVA runtime first determines its source and type. • If a listener for this type of event is registered with the source, an event object is created. • For each listener to this type of an event, the JAVA runtime invokes the appropriate event handling method to the listener and passes the event object as the parameter. Sources of Events • Button, Checkbox, Choice, List, Menu Item, Scrollbar, Text Components, Window, etc Event Handling Instructor: Tanzila Kehkashan 2
  • 3.
    Event Listener Interfacesand their Methods Event classes and their methods ActionListener • void actionPerformed (ActionEvent ae) ActionEvent Generated when a button is pressed, a list is double-clicked, or a menu item is selected. • String getActionCommand() returns string containing event source caption • Object getSource() ComponentListener • void componentResized (ComponentEvent ce) • void componentMoved (ComponentEvent ce) • void componentShown (ComponentEvent ce) • void componentHidden (ComponentEvent ce) ComponentEvent Generated when a component is hidden, moved, resized, or becomes visible. • String getComponent().getClass().getName() • Object getSource() FocusListener • void focusGained (FocusEvent fe) • void focusLost (FocusEvent fe) FocusEvent Generated when a component gains or loses keyboard focus. • String getComponent().getClass().getName() • Object getSource() • An interface is a group of related methods with empty bodies. • Event classes and their listener interfaces are available in java.awt.event Event Handling Instructor: Tanzila Kehkashan 3
  • 4.
  • 5.
    ASSIGNMENT Q#1: Write aprogram which has two buttons. After each click, the program determines how many times that specific button has been clicked. Q#2: Make online admission form of student in three steps. • In 1st frame, get personal bio-data of student, • In 2nd frame, get academic record of student, and • In 3rd frame, display all the information of student you collected in previous two frames. Instructor: Tanzila Kehkashan 5
  • 6.
    ASSIGNMENT Write a programwhich consists of two drop down Lists and four buttons having captions “>”, “>>”, “<”, “<<”. When user clicks on button “>”, selected item from left list moves towards right list. Rest of three buttons work accordingly. Instructor: Tanzila Kehkashan 6
  • 7.