{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

event driven programming

event driven programming - CSc 2700 Lecture 14 Introduction...

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 significant. 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 Document Right Arrow Icon
2 The Delegation Event Model The delegation event model is Java’s mechanism for handling events. Simply put, the delegation event model specifies 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. For 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 specifically 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. For example, when you click the printer button, a listener object sends a message to the printer. It’s not the button that notifies 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.
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.

{[ snackBarMessage ]}

Page1 / 8

event driven programming - CSc 2700 Lecture 14 Introduction...

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

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