1020244-Event Handling VIMP NOTES V2V
1020244-Event Handling VIMP NOTES V2V
• 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)
• When an event occurs, all registered listeners are notified and receive a copy of the event object. This is
known as multicasting the event.
• In all cases, notifications are sent only to listeners that register to receive them.
• Some sources may allow only one listener to register. The general form of such a method is this:
public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException
When such an event occurs, the registered listener is notified. This is known as unicasting the event.
• A source must also provide a method that allows a listener to unregister an interest in a specific type of
event. The general form of such a method is this:
public void removeTypeListener(TypeListener el)
• A listener is an object that is notified when an event occurs. It has two major requirements.
• First, it must have been registered with one or more sources to receive notifications about specific types of
events.
Second, it must implement methods to receive and process these notifications.
• The methods that receive and process events are defined in a set of interfaces found in java.awt.event.
• At the root of the Java event class hierarchy is EventObject, which is in java.util.
• It is the superclass for all events. Its one constructor is shown here: EventObject(Object src)
• The getSource( ) method returns the source of the event. Object getSource( )
• The class AWTEvent, defined within the java.awt package, is a subclass of EventObject. It is the superclass
(either directly or indirectly) of all AWT-based events used by the delegation event model.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• ActionEvent Generated when a button is pressed, a list item is double-clicked, or a menu item is selected.
• AdjustmentEvent Generated when a scroll bar is manipulated.
• ComponentEvent Generated when a component is hidden, moved, resized, or becomes visible.
• ContainerEvent Generated when a component is added to or removed from a container.
• FocusEvent Generated when a component gains or loses keyboard focus.
• InputEvent Abstract super class for all component input event classes.
• ItemEvent Generated when a check box or list item is clicked; also occurs when a choice selection is made
or a checkable menu item is selected or deselected.
• KeyEvent Generated when input is received from the keyboard.
• MouseEvent Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated
when the mouse enters or exits a component.
• MouseWheelEvent Generated when the mouse wheel is moved.
• TextEvent Generated when the value of a text area or text field is changed.
• WindowEvent Generated when a window is activated, closed, deactivated, deiconified, iconified, opened,
or quit.
• 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 that can be 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)
• You can obtain the command name for the invoking ActionEvent object by using the getActionCommand(
) method, String getActionCommand( )
• The getModifiers( ) method returns a value that indicates which modifier keys (ALT, CTRL, META, and/or
SHIFT) were pressed when the event was generated.
int getModifiers( )
• getWhen( ) that returns the time at which the event took place. This is called the event’s timestamp.
long getWhen( )
• Timestamps were added by ActionEvent to help support the improved input focus subsystem
• An AdjustmentEvent is generated by a scroll bar.
• The AdjustmentEvent class defines integer constants that can be used to identify • them.
BLOCK_DECREMENT: The user clicked inside the scroll bar to decrease its value.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
BLOCK_INCREMENT : The user clicked inside the scroll bar to increase its value.
TRACK :The slider was dragged.
UNIT_DECREMENT : The button at the end of the scroll bar was clicked to decrease its value.
UNIT_INCREMENT :The button at the end of the scroll bar was clicked to increase its value.
• AdjustmentEvent constructor:
AdjustmentEvent(Adjustable src, int id, int type, int data)
• The getAdjustable( ) method returns the object that generated the event. Adjustable getAdjustable( )
• The type of the adjustment event may be obtained by the getAdjustmentType( ) method.
• It returns one of the constants defined by AdjustmentEvent.
int getAdjustmentType( )
• The amount of the adjustment can be obtained from the getValue( ) method int getValue( )
• Constructors:
ComponentEvent(Component src, int type)
• The getComponent( ) method returns the component that generated the event. Component
getComponent( )
• constructor:
ContainerEvent(Component src, int type, Component comp)
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• You can obtain a reference to the container that generated this event by using the getContainer( ) method
Container getContainer( )
• The getChild( ) method returns a reference to the component that was added to or removed from the
container
Component getChild( )
• constructors:
FocusEvent(Component src, int type)
FocusEvent(Component src, int type, boolean temporaryFlag)
Focus Event(Component src, int type, boolean temporaryFlag, Component other)
• The isTemporary( ) method indicates if this focus change is temporary boolean isTemporary( )
• The abstract class InputEvent is a subclass of ComponentEvent and is the superclass for component input
events. Its subclasses are KeyEvent and MouseEvent.
• InputEvent defines several integer constants that represent any modifiers, such as the control key being
pressed, that might be associated with the event.
• InputEvent class defined the following eight values to represent the modifiers.
ALT_MASK
BUTTON2_MASK
META_MASK
ALT_GRAPH_MASK
BUTTON3_MASK
SHIFT_MASK
BUTTON1_MASK
CTRL_MASK
• To test if a modifier was pressed at the time an event is generated, use the isAltDown( ), isAltGraphDown(
), isControlDown( ), isMetaDown( ), and isShiftDown( ) methods.
• boolean isAltDown( )
• boolean isAltGraphDown( )
• boolean isControlDown( )
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• boolean isMetaDown( )
• boolean isShiftDown( )
• You can obtain a value that contains all of the original modifier flags by calling the getModifiers( ) method.
int getModifiers( )
• You can obtain the extended modifiers by called getModifiersEx( ), int getModifiersEx( )
• An ItemEvent is generated when a check box or a list item is clicked or when a checkable menu item is
selected or deselected.
•
DESELECTED :The user deselected an item.
SELECTED: The user selected an item.
• ItemEvent defines one integer constant, ITEM_STATE_CHANGED, that signifies a change of state.
• constructor:
ItemEvent(ItemSelectable src, int type, Object entry, int state)
• The getItem( ) method can be used to obtain a reference to the item that generated an event.
Object getItem( )
• The getItemSelectable( ) method can be used to obtain a reference to the ItemSelectable object that
generated an event.
ItemSelectable getItemSelectable( )
• Lists and choices are examples of user interface elements that implement the ItemSelectable interface.
• The getStateChange( ) method returns the state change (i.e., SELECTED or DESELECTED) for the event.
int getStateChange( )
• There are many other integer constants that are defined by KeyEvent.
• For example,
• VK_0 through VK_9 and VK_A through VK_Z define the ASCII equivalents of the
• numbers and letters. Here are some others:
• VK_ENTER
• VK_ESCAPE
• VK_CANCEL
• VK_UP
• VK_DOWN
• VK_LEFT
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• VK_RIGHT
• VK_PAGE_DOWN
• VK_PAGE_UP
• VK_SHIFT
• VK_ALT
• VK_CONTROL
• The VK constants specify virtual key codes and are independent of any modifiers, such as control, shift, or
alt.
• 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)
• getKeyChar( ), which returns the character that was entered, and getKeyCode( ), which returns the key
code.
• char getKeyChar( )
• int getKeyCode( )
• There are eight types of mouse events. The MouseEvent class defines the following
• integer constants that can be used to identify them:
• The most commonly used methods in this class are getX( ) and getY( ). These return the X and Y
coordinates of the mouse when the event occurred.
• int getX( )
• int getY( )
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• The getClickCount( ) method obtains the number of mouse clicks for this event.
int getClickCount( )
• isPopupTrigger( ) method tests if this event causes a pop-up menu to appear on this platform.
boolean isPopupTrigger( )
• int getButton( ) : It returns a value that represents the button that caused the event. The return value will be
one of these constants defined by MouseEvent.
NOBUTTON BUTTON1 BUTTON2 BUTTON3
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
If a mouse has a wheel, it is located between the left and right buttons. Mouse wheels are used for scrolling.
MouseWheelEvent defines these two integer constants.
• 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)
• If the value is positive, the wheel moved counterclockwise. If the value is negative, the wheel moved
clockwise.
• If the scroll type is WHEEL_UNIT_SCROLL, you can obtain the number of units to scroll by calling
getScrollAmount( ).
int getScrollAmount( )
• Text Event are generated by text fields and text areas when characters are entered by a user or program.
• Constructor:
TextEvent(Object src, int type)
• The TextEvent object does not include the characters currently in the text component that generated the
event.
• There are ten types of window events. The WindowEvent class defines integer constants that can be used to
identify them.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• Constructors:
• WindowEvent(Window src, int type)
• Window getOppositeWindow()
• int getOldState()
• int getNewState()
• ActionListener interface defines the actionPerformed( ) method that is invoked when an action event
occurs.
• void actionPerformed(ActionEvent ae)
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• ComponentListener interface defines four methods that are invoked when a component is resized, moved,
shown, or hidden.
• void componentResized(ComponentEvent ce)
• void componentMoved(ComponentEvent ce)
• void componentShown(ComponentEvent ce)
• void componentHidden(ComponentEvent ce)
• FocusListener interface defines two methods. When a component obtains keyboard focus, •
focusGained( ) is invoked. When a component loses keyboard focus, focusLost( ) is called.
• void focusGained(FocusEvent fe)
• void focusLost(FocusEvent fe)
• ItemListener interface defines the itemStateChanged( ) method that is invoked when the state of an item
changes.
• void itemStateChanged(ItemEvent ie)
• KeyListener interface defines three methods. The keyPressed( ) and keyReleased( ) methods are invoked
when a key is pressed and released, respectively. The keyTyped( ) method is invoked when a character has
been entered.
• MouseListener interface defines five methods. If the mouse is pressed and released at the same point,
mouseClicked( ) is invoked. When the mouse enters a component, the mouseEntered( ) method is called.
When it leaves, mouseExited( ) is called. The mousePressed( ) and mouseReleased( ) methods are invoked
when the mouse is pressed and released,
• void mouseClicked(MouseEvent me)
• void mouseEntered(MouseEvent me)
• void mouseExited(MouseEvent me)
• void mousePressed(MouseEvent me)
• void mouseReleased(MouseEvent me)
• MouseMotionListener interface defines two methods. The mouseDragged( ) method is called multiple
times as the mouse is dragged. The mouseMoved( ) method is called multiple times as the mouse is moved.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• MouseWheelListener interface defines the mouseWheelMoved( ) method that is invoked when the mouse
wheel is moved.
• void mouseWheelMoved(MouseWheelEvent mwe)
• TextListener interface defines the textChanged( ) method that is invoked when a change occurs in a text
area or text field
• void textChanged(TextEvent te)
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Advanced Java Programming Unit 3
• adapter class, that can simplify the creation of event handlers in certain situations. 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.
• an inner class is a class defined within other class, or even within an expression.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material