Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI."— Presentation transcript:

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

2 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

3 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).

4 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.

5 Event Handlers that We Will Study Listener interfaces and related classes Event ClassListener Interface MethodsAssociation MethodsGenerated by Class ActionEventinterface ActionListeneraddActionListenerJButton actionPerformed(ActionEvent)removeActionListenerJCheckBox JComboBox JTextField JRadioButton ItemEventinterface ItemListeneraddItemListenerJCheckBox itemStateChanged(ItemEvent)removeItemListenerJRadioButton JComboBox MouseEventinterface MouseListeneraddMouseListenergenerated by mouse mousePressed(MouseEvent)removeMouseListenerevent on any component mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent)

6 Using Mouse Events 1.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 )

7 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 #1 Step #2 Steps #3 Step #4

8 import java.awt.*; import java.awt.event.*; public class MouseEventDemo extends Applet implements MouseListener { String message; public void init() { message = “”; addMouseListener(this); } public void mousePressed(MouseEvent e) { message += "Mouse pressed”; } public void mouseReleased(MouseEvent e) { message = "Mouse released”; } public void mouseEntered(MouseEvent e) { message = "Mouse entered”; } public void mouseExited(MouseEvent e) { message = "Mouse exited”; } public void mouseClicked(MouseEvent e) { message = "Mouse clicked”; } MouseEvents

9 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


Download ppt "1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI."

Similar presentations


Ads by Google