Event Handling 2
Event Handling 2
• getOppositeComponent( )
Component getOppositeComponent( )
• isTemporary( )
- indicates if this focus change is temporary
- boolean isTemporary( )
6. The InputEvent class
• superclass for KeyEvent and MouseEvent classes
• subclass of ComponentEvent class
• Defines 8 values to represent the modifiers
• ALT_DOWN_MASK
• ALT_GRAPH_DOWN_MASK
• BUTTON1_DOWN_MASK
• BUTTON2_DOWN_MASK
• BUTTON3_DOWN_MASK
• CTRL_DOWN_MASK
• META_DOWN_MASK
• SHIFT_DOWN_MASK
• Methods
• boolean isAltDown( )
• boolean isAltGraphDown( )
• boolean isControlDown( )
• boolean isMetaDown( )
• boolean isShiftDown( )
these methods are used to test if a modifier was pressed at
the time an event is generated.
7. The ItemEvent class
• int getButton( ) -> return value that represents the button that
caused the event.
-> the return values will be one of these constants,
NOBUTTON, BUTTON1, BUTTON2, BUTTON3
10. The MouseWheelEvent class
• Generated when the mouse wheel is moved.
• subclass of MouseEvent
• Integer constants
• WHEEL_BLOCK_SCROLL -> page-up or page-down scroll
• WHEEL_UNIT_SCROLL -> line-up or line-down scroll
• Constructor
MousewheelEvent(Component src, int type, long when, int modifiers, int x, int
y, int clicks, boolean triggersPopup, int scrollHow, int amount, int count)
ScrollHow -> either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_SCROLL
amount -> no.of units to scroll
count -> no.of rotational units that the wheel moved
Methods
• int getWheelRotation( )
• to obtain the no.of rotational units
• value -> positive -> wheel moved counter clockwise
negative -> wheel moved clockwise
• int getScrollType( )
• int getScrollAmount( )
11. WindowEvent class
• Generated when a window is activated, closed, deactivated,
deiconified, iconified, opened or quit.
• 10 types of window events
• subclass of ComponentEvent
• 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)
other -> opposite window when a focus event occurs
fromState -> prior state of the window
toState -> new state of the window
Methods
• Window getWindow( )
• Window getOppositeWindow( )
• int getOldState( )
• int getNewState( )
12. TextEvent class
• Generated when the value of a text area or text field is changed.
• Integer constant
TEXT_VALUE_CHANGED
• Constructor
TextEvent(Object src, int type)
Sources of Events
Button
Checkbox
Choice
List
Menu item
Scroll bar
Text components
Window
EventListener Interfaces
ActionListener Interface
defines one method to receive action events
void actionPerformed(ActionEvent ae)
AdjustmentListener Interface
defines one method to receive adjustment events
void adjustmentValueChanged(AdjustmentEvent ae)
ComponentListener Interface
defines 4 methods to recognize when a component is hidden, moved, resized
or shown
void ComponentHidden(ComponentEvent ce)
void ComponentMoved(ComponentEvent ce)
void ComponentResized(ComponentEvent ce)
void ComponentShown(ComponentEvent ce)
ContainerListener Interface
defines 2 methods to recognize when a component is added or removed from
a container
void ComponentAdded(ContainerEvent ce)
void ComponentRemoved(ContainerEvent ce)
FocusListener Interface
defines 2 methods to recognize when a component gains or loses keyboard
focus
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)
ItemListener Interface
defines one method to recognize when the state of an item changes
void itemStateChanged(ItemEvent ie)
KeyListener Interface
defines 3 methods to recognize when a key is pressed, released or typed.
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
MouseListener Interface
defines 5 methods
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
MouseWheelListener Interface
defines one method to recognize when the mouse wheel is moved
void mouseWheelMoved(MouseWheelEvent mwe)
TextListener Interface
defines one method to recognize when a text value changes
void textChanged(TextEvent te)
WindowFocusListener Interface
defines 2 methods to recognize when a window gains or loses input focus
void windowGainedFocus(WindowEvent we)
void windowLostFocus(WindowEvent we)
WindowListener Interface
defines 7 methods
void windowActivated(WindowEvent we)
void windowClosed(WindowEvent we)
void windowClosing(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowIconified(WindowEvent we)
void windowOpened(WindowEvent we)
Adapter Classes
• Simplify the creation of event handlers in certain situations.
• useful when you want to receive and process only some of the events
that are handled by a particular event listener interface.
• eg. the MouseMotionAdapter class has two methods,
mouseDragged( ) and mouseMoved( )
• signatures same as defined in MouseMotionListener interface
• if only interested in mouse drag events, simply extend
MouseMotionAdapter and implement mouseDragged( )
• The commonly used adapter classes in java.awt.event are,
• ComponentAdapter
• ContainerAdapter
• FocusAdapter
• KeyAdapter
• MouseAdapter
• MouseMotionAdapter
• WindowAdapter