We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Event handling
• Events are supported by a number of
packages, including java.util, java.awt, and java.awt.event. • There are several types of events, including those generated by the mouse, the keyboard, and various GUI controls, such as a push button, scroll bar, or check box. Two Event Handling Mechanisms • The Delegation Event Model: • a source generates an event and sends it to one or more listeners. • the listener simply waits until it receives an event. • Once an event is received, the listener processes the event and then returns. • listeners must register with a source in order to receive an event notification. • an event is an object that describes a state change in a source. Handling Mouse Events • See word file Adapter class • An adapter class provides an empty implementation of all methods in an event listener interface. • Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. • You can define a new class to act as an event listener by extending one of the adapter classes and implementing only those events in which you are interested. • For example, the MouseMotionAdapter class has two methods, mouseDragged( ) and mouseMoved( ), which are the methods defined by the MouseMotionListener interface. • If you were interested in only mouse drag events, then you could simply extend MouseMotionAdapter and override mouseDragged( ). • The empty implementation of mouseMoved( ) would handle the mouse motion events for you. • See the program on word file