CH 5
CH 5
DRIVEN
PROGRAMMING
DELEGATION EVEN MODEL
• Event handling is most important in applet programming.
• Most events to which an applet will respond are generated by the user.
• There are several types of events.
• The most common events are generated by the mouse, the keyboard, and various
controls, such as a push button.
• Events are supported by the java.awt.event package.
• The Delegation Event Model:
• The Delegation event model is an event handling mechanism.
• In this scheme, a source generates an event and sends it to one or more listeners.
• The listener waits until it receives an event. Once received, the listener processes the
event and then returns.
• The advantage of this design is that the application logic that processes events is totally
separated from the user interface logic that generates those events.
• A user interface element is able to “delegate” the processing of an event to a separate
piece of code.
• The listeners must register with a source in order to receive an event notification.
• So, notifications are sent only to listeners that want to receive them.
• Previously, an event was propagated through the hierarchy until it was handled by a
component, which wasted valuable time.
• The delegation event model eliminates this overhead.
EVENTS AND EVENT SOURCES
• Events:
• An event is an object that describes a change of state in a source.
• Event is generated as a result of a person interaction with the elements
in a graphical user interface, like, pressing a button, entering a character
via the keyboard, selecting an item in a list, and clicking the mouse.
• Events may also occur that are not directly generated by interactions
with a user interface.
• An event may be generated when a timer expires, a counter exceeds a
value, software or hardware failure occurs, or an operation is completed.
• Event Sources:
• A source is an object that generates an event. This occurs when the
internal state of that object changes in some way.
• Sources may generate more than one type of event.
• A source must register listeners in order for the listeners to receive
notifications about a specific type of event.
• Each type of event has its own registration method.
• The general form:
public void addTypeListener(TypeListener el)
EVENT LISTENERS
• Here, Type is the name of the event and el is a reference to the event listener.
• For example, addKeyListener( ), addMouseMotionListener( ).
• When an event occurs, all registered listeners are notified and receive a copy of the event
object. This is known as multicasting the event.
• Some sources may allow only one listener to register. So, the method to add listener can
throw TooManuListenersException.
• public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException
• When such an event occurs, the only one registered listener is notified.
• This is known as unicasting the event.
• A listener can unregister from a specific type of event using removelistner method. The
general form of such a method is this:
public void removeTypeListener(TypeListener el) For Example, removeKeyListener( )
• The methods that add or remove listeners are provided by the source that generates events.
• Event Listeners:
• A listener is an object that is notified when an event occurs.
• Listeners must have been registered with one or more sources to receive notifications about
specific types of events.
• Listener must implement methods to receive and process these notifications.
• The methods that receive and process events are defined in interfaces of java.awt.event.
Event Classes
• These classes represent events and are at the core of Java’s event
handling mechanism.
• At the root of the Java event class hierarchy is EventObject, which is in
java.util package.
• It is the superclass for all events.
• Its one constructor is shown here:
EventObject(Object src)
• Here, src is the object that generates this event.
• EventObject class contains two methods: getSource( ) and toString( ).
• The getSource( ) method returns the source of the event.
• The toString ( ) returns the string equivalent of the event object.
• The class AWTEvent, defined within the java.awt package, is a subclass
of EventObject.
• It is the superclass of all AWT-based events.
• Its getID( ) method can be used to determine the type of the event.
• EventObject is a superclass of all events.
• AWTEvent is a superclass of all AWT events that are handled by the
delegation event model.
EVENT CLASSES OF java.awt.event
THE ActionEvent CLASS
• An ActionEvent is generated when a button is pressed; a list item is double-clicked, or a menu item
is selected.
• The ActionEvent class defines four integer constants.
• Those are used to identify any modifiers associated with an action event: ALT_MASK, CTRL_MASK,
META_MASK, and SHIFT_MASK.
• In addition, there is an integer constant, ACTION_PERFORMED, which can be used to identify action
events.
• Constructors:
• ActionEvent(Object src, int type, String cmd) ActionEvent(Object src, int type, String cmd, int
modifiers)
• ActionEvent(Object src, int type, String cmd, long when, int modifiers)
• src is a reference to the source object that generated this event.
• The type of the event is specified by type and its command string is cmd.
• The argument modifiers indicate which modifier keys (ALT, CTRL, META, and/or SHIFT) were pressed
when the event was generated. The when parameter specifies when the event occurred.
• Methods:
• getActionCommand( ) : To obtaine the Command name. For example, when a button is pressed, an
action event is generated that has a command name equal to the label on that button.
• getModifiers( ) : Returns a value that indicates which modifier keys (ALT, CTRL, META, and/or SHIFT)
were pressed when the event was generated.
• getWhen( ) : Returns the time at which the event took place. This is called the event’s timestamp.
THE AdjustmentEvent CLASS
• An AdjustmentEvent is generated by a scroll bar.
• There are five types of adjustment events. The AdjustmentEvent class has integer constants
that are used to identify them.
• BLOCK_DECREMENT The user clicked inside the scroll bar to decrease its value.
• BLOCK_INCREMENT The user clicked inside the scroll bar to increase its value.
• TRACK The slider was dragged.
• UNIT_DECREMENT The button at end of scrollbar was clicked to decrease value.
• UNIT_INCREMENT The button at end of scrollbar was clicked to increase its value.
• One more integer costant,
• ADJUSTMENT_VALUE_CHANGED indicates that a change has occurred.
• Constructor:
• AdjustmentEvent(Adjustable src, int id, int type, int data)
• Where, src is a reference to the object that generated this event.
• The id equals ADJUSTMENT_VALUE_CHANGED.
• The type of the event is specified by type, and its associated data is data.
• Methods:
• Adjustable getAdjustable( ) : Returns the object that generated the event.
• int getAdjustmentType( ) : Returns type of the adjustment event.
• int getValue( ) : Returns amount of the adjustment can be obtained from the method.
THE ComponentEvent CLASS
• A ComponentEvent is generated when the size, position, or visibility of a
component is changed.
• There are four types of component events.
• The ComponentEvent class defines integer constants that can be used to
identify them. The constants and their meanings are shown here:
• COMPONENT_HIDDEN The component was hidden.
• COMPONENT_MOVED The component was moved.
• COMPONENT_RESIZED The component was resized.
• COMPONENT_SHOWN The component became visible.
• Constructor:
• ComponentEvent(Component src, int type)
• Where,src is a reference to the object that generated event. Second
argument specifies event type.
• ComponentEvent is the superclass of ContainerEvent, FocusEvent, KeyEvent,
MouseEvent, and WindowEvent.
• The getComponent( ) method returns the component that generated the
event.
THE ContainerEvent CLASS
• A ContainerEvent is generated when a component is added to
or removed from a container.
• There are two types of container events. The ContainerEvent
class defines int constants that can be used to identify them.
• COMPONENT_ADDED and COMPONENT_REMOVED.
• ContainerEvent is a subclass of ComponentEvent.
• Constructor:
• ContainerEvent(Component src, int type, Component comp)
• src is a reference to the container that generated this event.
• The type of the event is specified by type, and the component
that has been added to or removed from the container is comp.
• Methods:
• Container getContainer( ) : It is used to obtain a reference to
the container that generated this event.
• Component getChild( ) : It returns a reference to the
component that was added to or removed from the container.
THE FocusEvent CLASS
• A FocusEvent is generated when a component gains or loses input focus.
• These events are identified by the integer constants FOCUS_GAINED and FOCUS_LOST.
• FocusEvent is a subclass of ComponentEvent.
• Constructors:
• FocusEvent(Component src, int type)
• FocusEvent(Component src, int type, boolean temporaryFlag)
• Focus Event(Component src, int type, boolean temporaryFlag, Component other)
• The src is a reference to the component that generated this event.
• The type of the event is specified by type.
• The argument temporaryFlag is set to true if the focus event is temporary.
• Otherwise, it is set to false. (A temporary focus event occurs as a result of another user
interface operation. For example, assume that the focus is in a text field. If the user
moves the mouse to adjust a scroll bar, the focus is temporarily lost.)
• If a FOCUS_GAINED event occurred, other will refer to the component that lost focus. If
FOCUS_LOST event occurred, other will refer to the component that gains focus.
• Methods:
• getOppositeComponent( ) : Returns reference to the other component.
• isTemporary( ) : It indicates if this focus change is temporary. The method returns true if
the change is temporary. Otherwise, it returns false.
THE Itemevent CLASS
• An ItemEvent is generated when a check box or a list item is clicked or when a checkable
menu item is selected or deselected.
• There are two types of item events which are identified by the following integer
constants:
• DESELECTED The user deselected an item.
• SELECTED The user selected an item.
• It defines one more integer constant, ITEM_STATE_CHANGED, it signifies change of state.
• Constructor:
• ItemEvent(ItemSelectable src, int type, Object entry, int state)
• src is a reference to the component that generated this event.
• The type of the event is specified by type. The current state of that item is in state.
• The specific item that generated the item event is passed in entry.
• Methods:
• getItem( ): To obtain a reference to the item that generated an event. Object getItem( )
• getItemSelectable( ): To obtain a reference to the ItemSelectable object that generated
an event. ItemSelectable getItemSelectable( )
• The getStateChange( ) : It returns the state change (i.e., SELECTED or DESELECTED) for
the event. int getStateChange( )
THE KeyEvent CLASS
• A KeyEvent is generated when keyboard input occurs.
• There are three types of key events, which are identified by these integer constants: KEY_PRESSED,
KEY_RELEASED, and KEY_TYPED.
• There are many other integer constants that are defined by KeyEvent.
• VK_0 to VK_9 and VK_A to VK_Z define the ASCII equivalents of the numbers and letters.
• VK_ENTER , VK_ESCAPE, VK_CANCEL, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT VK_PAGE_DOWN,
VK_PAGE_UP, VK_SHIFT, VK_ALT, VK_CONTROL. VK - virtual key codes and are independent of any
modifiers. KeyEvent is a subclass of InputEvent.
• Constructors:
• KeyEvent(Component src, int type, long when, int modifiers, int code)
• KeyEvent(Component src, int type, long when, int modifiers, int code, char ch)
• src is a reference to the component that generated the event. The type of the event is specified by type.
• The system time at which the key was pressed is passed in when.
• The modifiers argument indicates which modifiers were pressed when this key event occurred.
• The virtual key code, such as VK_UP, VK_A, and so forth, is passed in code.
• The character equivalent (if one exists) is passed in ch. If no valid character exists, then ch contains
CHAR_UNDEFINED. For KEY_TYPED events, code will contain VK_UNDEFINED.
• Methods:
• getKeyChar( ): Returns the character that was entered.
• char getKeyChar( ): If no valid character is available, then getKeyChar( ) returns CHAR_UNDEFINED.
• getKeyCode( ): Returns the key code.
• int getKeyCode( ): When a KEY_TYPED event occurs, getKeyCode( ) returns VK_UNDEFINED.
THE MouseEvent CLASS
• There are eight types of mouse events. MouseEvent is a subclass of InputEvent.
• MOUSE_CLICKED The user clicked the mouse.
• MOUSE_DRAGGED The user dragged the mouse.
• MOUSE_ENTERED The mouse entered a component.
• MOUSE_EXITED The mouse exited from a component.
• MOUSE_MOVED The mouse moved.
• MOUSE_PRESSED The mouse was pressed.
• MOUSE_RELEASED The mouse was released.
• MOUSE_WHEEL The mouse wheel was moved.
• Constructors:
• MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup)
• Where, src is a reference to the component that generated the event. The type of the event is specified by type.
• The system time at which the mouse event occurred is passed in when.
• The modifiers argument indicates which modifiers were pressed when a mouse event occurred.
• The coordinates of the mouse are passed in x and y. The click count is passed in clicks.
• The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform.
• Methods:
• getX( ) and getY( ): These return the X and Y coordinates of the mouse when the event occurred.
• getPoint( ) : It is used to obtain the coordinates of the mouse.
• Point getPoint( ): It returns a Point object that contains the X, Y coordinates in its integer members: x and y.
• getClickCount( ) : This method obtains the number of mouse clicks for this event.
• isPopupTrigger( ) : This method tests if this event displays a pop-up menu.
• getButton( ) : This method returns a value that represents the button that caused the event.
– NOBUTTON BUTTON1
– BUTTON2 BUTTON3
THE MouseWheelEvent CLASS
• The MouseWheelEvent class encapsulates a mouse wheel event. It is a subclass of MouseEvent .
• WHEEL_BLOCK_SCROLL A page-up or page-down scroll event occurred.
• WHEEL_UNIT_SCROLL A line-up or line-down scroll event occurred.
• Constructor:
• MouseWheelEvent(Component src, int type, long when, int modifiers,
• int x, int y, int clicks, boolean triggersPopup,
• int scrollHow, int amount, int count)
• src is a reference to the object that generated the event. The type of the event is specified by type.
• The system time at which the mouse event occurred is passed in when.
• The modifiers argument indicates which modifiers were pressed when the event occurred.
• The coordinates of mouse are passed in x and y. The number of clicks wheel has rotated is passed in clicks.
• The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform.
• The scrollHow value must be either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_SCROLL.
• The number of units to scroll is passed in amount.
• The count parameter indicates the number of rotational units that the wheel moved.
• Methods:
• getWheelRotation( ) : To obtain the number of rotational units.
• int getWheelRotation( ): If the number of rotation is positive, the wheel moved counterclockwise.
• If the value is negative, the wheel moved clockwise.
• getScrollType( ) : To obtain the type of scroll.
• int getScrollType( ): It returns either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_SCROLL.
• int getScrollAmount( ) : To obtain the number of units, if the scroll type is WHEEL_UNIT_SCROLL.
THE TextEvent CLASS
• This class describes text events, which are
generated by text fields and text areas when
characters are entered by a user or program.
• TextEvent defines the integer constant
TEXT_VALUE_CHANGED.
• Constructor :
• TextEvent(Object src, int type)
• src is a reference to the text object that
generated this event.
• The type of the event is specified by type.
THE WindowEvent CLASS
• There are ten types of window events. WindowEvent is a subclass of ComponentEvent.
• WINDOW_ACTIVATED The window was activated.
• WINDOW_CLOSED The window has been closed.
• WINDOW_CLOSING The user requested that the window be closed.
• WINDOW_DEACTIVATED The window was deactivated.
• WINDOW_DEICONIFIED The window was deiconified.
• WINDOW_GAINED_FOCUS The window gained input focus.
• WINDOW_ICONIFIED The window was iconified.
• WINDOW_LOST_FOCUS The window lost input focus.
• WINDOW_OPENED The window was opened.
• WINDOW_STATE_CHANGED The state of the window changed.
• Constructors:
• WindowEvent(Window src, int type) WindowEvent(Window src, int type, Window other)
• WindowEvent(Window src, int type, int fromState, int toState)
• WindowEvent(Window src, int type, Window other, int fromState, int toState)
• src is a reference to the object that generated this event. The type of the event is type.
• The other specifies opposite window when focus event occurs. fromState specifies prior state of window.
• The toState specifies the new state that the window will have when a window state change occurs.
• Methods:
• getWindow( ) : Returns the Window object that generated the event.
• Window getOppositeWindow(): Returns the opposite window.
• int getOldState() : Returns the previous window state.
• int getNewState(): Returns the current window state.
THE InputEvent CLASS
• The abstract class InputEvent is a subclass of
ComponentEvent and is the superclass for
component input events.
• Its subclasses are KeyEvent and MouseEvent.
EVENT LISTENER INTERFACES
• The delegation event model has two parts: sources and listeners.
• Listeners are created by implementing the interfaces defined by java.awt.event package.
• When an event occurs, the event source invokes the appropriate method defined by the
listener.
• These methods take appropriate event class object as argument.
• The ActionListener Interface:
• This interface defines the actionPerformed( ) method that is
invoked when an action event occurs.
void actionPerformed(ActionEvent ae)