1 event handling – lecture 4 prepared by: ahmad ramin rahimee assistant professor icti

9
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

Upload: osborn-horton

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Basic Event Handling The GUI is responsible for constructing the user interface and for connecting (registering) widgets to listeners The listener part implements the appropriate interface for the type of event(s) of interest. The code (in the action handler) that performs the program’s action associated with the event(s).

TRANSCRIPT

Page 1: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

1

Event Handling – Lecture 4

Prepared by: Ahmad Ramin RahimeeAssistant ProfessorICTI

Page 2: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

What is an Event in Java?• Defn: - in programming, an event is (in most cases) an action taken by the user.

• Types of events:

1) the user presses a key or clicks the mouse button.2) the user clicks on a button, a choice, a checkbox, etc.

• Thus, rather than a program be executed as a long sequence of instructions, like the following:

• We have sections of the program which can be activated by the occurrence of an event.

Sequential Execution

Page 3: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

Basic Event Handling

• The GUI is responsible for constructing the user interface and for connecting (registering) widgets to listeners

• The listener part implements the appropriate interface for the type of event(s) of interest.

• The code (in the action handler) that performs the program’s action associated with the event(s).

Page 4: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

The Event Loop

• In Java, the programmer creates a section(s) of code (methods) which will be automatically invoked whenever an event happens.

• Details of the event (the name of the button that was pressed, the choice box that was changed, the radio button that was pressed, etc) are returned to the programmer so that the required processing takes place.

• In Java, events are classified into several classes (we will study the following):

1) ActionEvents - generated whenever a change is made to a JButton, JCheckBox, JComboBox, JTextField, or JRadioButton..

2) MouseEvents - generated whenever mouse moves, being clicked and etc...3) ItemEvents - generated whenever a change is made to a JButton,

JCheckBox, JComboBox, or JRadioButton.

Page 5: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

Event Handlers that We Will Study

Listener interfaces and related classes

Event Class Listener Interface Methods Association Methods Generated by ClassActionEvent interface ActionListener addActionListener JButton  actionPerformed(ActionEvent) removeActionListener JCheckBox      JComboBox      JTextField      JRadioButtonItemEvent interface ItemListener addItemListener JCheckBox  itemStateChanged(ItemEvent) removeItemListener JRadioButton      JComboBox       MouseEvent interface MouseListener addMouseListener generated by mouse  mousePressed(MouseEvent) removeMouseListener event on any component  mouseReleased(MouseEvent)      mouseEntered(MouseEvent)      mouseExited(MouseEvent)      mouseClicked(MouseEvent)    

Page 6: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

Using Mouse Events1. Import the required event classes; thus giving your applet the power to receive and handle messages, such as those

sent when a button is clicked or the mouse is moved.import java.awt.event.*;

2. State that the class implements an MouseListener.

public class EventHandlingPractice extends Applet implements MouseListener

3. Inform the applet that this object will respond to mouse events, using addMouseListener.

addMouseListener(this);

4. Provide a method called mouseClicked() to be invoked when a mouse event occurs.

public void mouseClicked( MouseEvent e )

Page 7: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

What are the Parts?import java.awt.*;import java.awt.event.*;

public class IRS extends Applet implements MouseListener{

public void init() {

addMouseListener(this); }

public void mouseClicked( MouseEvent e ){

}}

Step #1Step #2

Steps #3

Step #4

Page 8: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

MouseEvents

Page 9: 1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

• Mouse events are generated by the following types of user interaction:– A mouse click– A mouse entering a component's area– A mouse leaving a component's area

• Any component can generate these events, and a class must implement the MouseListener interface to support them.

• Methods:• void mouseClicked(MouseEvent e) – invoked when the mouse button has been clicked (pressed and released) on a

component.

• void mouseDragged(MouseEvent e) – invoked when a mouse button is pressed on a component and then dragged.

• void mouseEntered(MouseEvent e) - Invoked when the mouse enters a component.

• void mouseExited(MouseEvent e) - Invoked when the mouse exits a component.

• void mouseMoved(MouseEvent e) - Invoked when the mouse cursor has been moved onto a component.

• void mousePressed(MouseEvent e) - Invoked when a mouse button has been pressed on a component.

• void mouseReleased(MouseEvent e) - Invoked when a mouse button has been released on a component.

MouseEvents