04-Events - 1 1 Event Handling (Part 1) 2 Why Use Events...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 1 Event Handling (Part 1) 2 Why Use Events for GUI Input? Console I/O uses blocking procedure calls System controls the dialog GUI input uses event handling User has much more control over the dialogue User can click on almost anything 2 3 Event Handling For the user to interact with a GUI, the underlying operating system must support event handling Operating systems constantly monitor events such as keystrokes, mouse clicks, ink input, voice command, etc. Operating systems sort out these events and report them to the appropriate application programs Each application program then decides what to do in response to these events 4 Complexity vs. Power Visual Basic Each component responds to a fixed set of events C A giant loop with a massive switch statement Java Event delegation model events are transmitted from event sources to event listeners You can designate any object to be an event listener 3 5 Event Queue Events are stored in a queue Queue saves application from hard real time constraints (i.e., having to finish handling each event before next one might occur) Mouse moves are coalesced into a single event in queue If application cant keep up, then sketched lines have very few points 6 Event Loop While application is running Block until an event is ready Get event from queue Translate lower-level events into higher-level events Translated events are put into the queue Dispatch event to target component 4 7 Event Dispatch and Propagation Dispatch: choose target component for event Key event: component with keyboard focus Mouse event: component under mouse Propagation: if target component declines to handle event, the event passes up to its parent 8 Designing a Controller A controller is a finite state machine Example: button 5 9 AWT Event Handling A listener object is an instance of a class that implements a special interface called a listener interface An event source is an object that can register listener objects and send them event objects The event source sends out event objects to all registered listeners when that event occurs The listener objects will then use the information in the event object to determine their reaction to the event 10 AWT Event Handling Register listener object with the event source object eventSourceObject. add Event Listener( eventListenerObject ); For example ActionListener listener = ; JButton button = new JButton(OK); button.addActionListener(listener); The listener object is notified whenever an action event occurs in the button (i.e., a button click) 6...
View Full Document

This note was uploaded on 02/22/2011 for the course CSE 333 taught by Professor Zao during the Fall '10 term at SUNY Stony Brook.

Page1 / 19

04-Events - 1 1 Event Handling (Part 1) 2 Why Use Events...

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

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