event driven programming

event driven programming - CSc 2700: Lecture # 14...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSc 2700: Lecture # 14 Introduction to Event-driven Programming October 16, 2008 1 Introduction In terms of programming, an event is an occurrence. Pressing a button, selecting a check box, choosing an item from a menu, or simply moving the mouse are all events. For example, clicking the × button that you see in the upper right hand corner of a Windows window generates an event. The system handles this event by closing the window. In a word processing Figure 1: Clicking a Button Generates an Event environment, clicking on the button with the printer icon also generates an event. The event is handled by sending a document to the printer. An application can ignore or handle an event. In any program, many events occur but only some are signi±cant. For example, each mouse click generates an event but only some clicks warrant a response. Programs that respond to events are called event-driven programs. Almost all popular com- mercial programs are event-driven, including word processors, video games, and spreadsheets. In this and succeeding lectures, we will further discuss Event-driven programming. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 The Delegation Event Model The delegation event model is Java’s mechanism for handling events. Simply put, the delegation event model speciFes that: when an event is generated by some source such as a button or the mouse, the response is delegated or handed over to some other object. ±or example, when a user presses an Exit button (the source), the button object does not close the application; another object carries out the response. The source creates the event, an ”event object,” and then sends or passes the event object to another object for processing. More speciFcally Whenever an event is generated, an event object belonging to the Even- tObject class is automatically instantiated. This event object encap- sulates information about the event including the source of the event – a button, the mouse, a check box, a menu item – along with other pertinent information such as the number of mouse clicks, the current screen position of the mouse, or whether or not a check box is checked. The source object passes the event object to one or more listeners. A listener is an object with methods that process or handle the event. The listeners do the work. ±or example, when you click the printer button, a listener object sends a message to the printer. It’s not the button that notiFes the printer; a listener does the work for the button. When you click an Exit button, a listener issues the command such as System.exit(0). The listener is a servant, patiently waiting to respond to events. A listener object waits until a source passes an event to it. Once the listener receives an event, the listener responds to the event. Thus, the principal actors of the event delegation model are three: the source,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/06/2009 for the course CSC 2700 taught by Professor Duncan during the Spring '08 term at LSU.

Page1 / 8

event driven programming - CSc 2700: Lecture # 14...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online