lecture16

lecture16 - Introduction to Computation and Problem Solving...

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

View Full Document Right Arrow Icon
1 Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 16: Class 16: The Swing Event Model The Swing Event Model 2 Event Driven Programming, 1 door bell remote door release remote door button 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
3 Event Driven Programming, 2 In event driven programming, the user controls what happens next. A user's action (button press, typing, mouse drag) triggers a program response like a visitor's ringing the doorbell triggers us to open the door. How can we pass alerts or other information (events) from one part of the program to another? How can we implement the response? Put another way, how do we wire up our application to make it interactive? 4 The Java Event Model present information (with one exception) something? field occurs Up until now, we have focused on GUI's to How do GUIs interact with users? How do applications recognize when the user has done In Java this depends on 3 related concepts: events: objects that represent a user action with the system event sources: in Swing, these are components that can recognize user action, like a button or an editable text event listeners: objects that can respond when an event 2
Background image of page 2
5 Event Delegation events ( AWTEvent objects) when user does UI components ( event sources to subscribe to these events so, when an ( the event listener ). Java runtime system generates something with mouse or keyboard ) allow you event you are interested in happens, a method in your code is called. The component delegates handling the event to the object containing the method you supply 6 Event Sources Event sources can generate events. JComponents like JButtons and JPanels The ones you will be most interested are subclases of You find out the kind of events they can generate by reading the Javadoc 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
7 Event Listeners example: public interface ActionListener } event source the type of listener An object becomes an event listener when its class implements an event listener interface. For extends EventListener { public void actionPerformed(ActionEvent e); The event listener gets called when the event occurs if we register the event listener with the the method you must implement 8 Events For example, instances of MouseEvent have getX() and getY() occurred. All event listener methods take an event as an argument. Events are instances of simple classes that supply information about what happened. methods that will tell you where the mouse event (e.g., mouse press) 4
Background image of page 4
9 How do I Set Up to Receive an Event? 0. Import java.awt.event.*; 1. 2. handle the event. 3. 4. class of the handler object. 5. Call an add EventType Listener() register the listener with the event source Figure out what type of event you are interested in and what component it comes from. Decide which object is going to
Background image of page 5

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

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

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 16

lecture16 - Introduction to Computation and Problem Solving...

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