4. Prepared By: Asst. Prof. Sejal Jadav
Event Classes
and
Listener Interfaces
5. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Event Classes Listener Interfaces
ActionEvent ActionListener
ItemEvent ItemListener
FocusEvent FocusListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
TextEvent TextListener
KeyEvent KeyListener
6. Prepared By: Asst. Prof. Sejal Jadav
Q-8 Explain FocusEvent Class.
ā¢ A FocusEvent is generated when a component gains or loses input
focus.
FOCUS_GAINED
ā¢ It is set when the component gains the input focus.
FOCUS_LOST
ā¢ It is set when the component loses the input focus.
7. Prepared By: Asst. Prof. Sejal Jadav
ā¢ FocusEvent is a subclass of ComponentEvent and has these
constructors:
ā¢ FocusEvent(Component src, int type)
ā¢ FocusEvent(Component src, int type, boolean temporaryFlag)
ā¢ Here, 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.
8. Prepared By: Asst. Prof. Sejal Jadav
ā¢ (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.)
9. Prepared By: Asst. Prof. Sejal Jadav
ā¢ The isTemporary( ) method indicates if this focus change is temporary.
Its form is shown here:
boolean isTemporary( )
ā¢ The method returns true if the change is temporary. Otherwise, it
returns false.
10. Prepared By: Asst. Prof. Sejal Jadav
Q: 15 The FocusListener Interface:
ā¢ This interface defines two methods which are called when a component
gets or loses the focus.
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)
ā¢ Example: AwtListenerDemo.java
11. Prepared By: Asst. Prof. Sejal Jadav
Q: Java Adapter Classes
ā¢ Java adapter classes provide the default implementation of listener
interfaces.
ā¢ If you inherit the adapter class, you will not be forced to provide the
implementation of all the methods of listener interfaces.
ā¢ So it saves code.
12. Prepared By: Asst. Prof. Sejal Jadav
Swing Focus Adapter
ā¢ 1. FocusAdapter class is an abstract adapter class in Java Swing.
ā¢ 2. FocusAdapter class is for receiving keyboard focus events.
ā¢ 3. Methods specified in FocusAdapter class are empty.
ā¢ 4. FocusAdapter class exists as convenience(suitable) for creating
listener objects
13. Prepared By: Asst. Prof. Sejal Jadav
Class declaration
ā¢ Below is the declaration for java.awt.event.FocusAdapter class ā
public abstract class FocusAdapter extends Object
implements FocusListener
14. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Class constructors: FocusAdapter()
Class methods:
No. Method Return Type Parameter Invoked when
1 focusGained() void FocusEvent
component gains the
keyboard focus
2 focusLost() void FocusEvent
component loses the
keyboard focus
15. Prepared By: Asst. Prof. Sejal Jadav
Methods inherited:
ā¢ This class inherits methods from the following classes:
java.lang.Object
ļ¶Swing Focus Adapter Example: AdapterExample.java
16. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Event Classes Listener Interfaces
ActionEvent ActionListener
ItemEvent ItemListener
FocusEvent FocusListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
TextEvent TextListener
KeyEvent KeyListener
17. Prepared By: Asst. Prof. Sejal Jadav
Q-10 Explain 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
ā¢ It is set when a key is pressed in the keyboard.
KEY_RELEASED
ā¢ It is set when a key is pressed in the keyboard.
18. Prepared By: Asst. Prof. Sejal Jadav
KEY_TYPED
ā¢ It is set when a printable key is pressed. The key such as ALT, CTRL,
SHIFT, etc are not printable character.
19. Prepared By: Asst. Prof. Sejal Jadav
ā¢ 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:
20. Prepared By: Asst. Prof. Sejal Jadav
Here are some others:
ā¢ The VK constants specify virtual key codes and are independent of any
modifiers, such as control, shift, or alt.
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
21. Prepared By: Asst. Prof. Sejal Jadav
ā¢ KeyEvent is a subclass of InputEvent. Here are two of its 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)
22. Prepared By: Asst. Prof. Sejal Jadav
ā¢ Here, 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.
23. Prepared By: Asst. Prof. Sejal Jadav
ā¢ 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.
24. Prepared By: Asst. Prof. Sejal Jadav
The most commonly used methods of KeyEvent class are:
char getKeyChar( )
ā¢ It returns the character that was pressed entered.
getKeyCode( )
ā¢ It returns the code of the key pressed.
25. Prepared By: Asst. Prof. Sejal Jadav
Q: 15) The KeyListener Interface:
ā¢ This interface defines three methods.
void keyPressed(KeyEvent ke)
ā¢ This method is called when a keyboard key is pressed.
void keyReleased(KeyEvent ke)
ā¢ This method is called when a keyboard key is released.
void keyTyped(KeyEvent ke)
ā¢ This method is called when a printable character key is pressed.
27. Prepared By: Asst. Prof. Sejal Jadav
Java KeyAdapter Example [page- 24]
ā¢ The class KeyAdapter is an abstract (adapter) class for receiving
keyboard events.
ā¢ All methods of this class are empty. This class is convenience class for
creating listener objects.
28. Prepared By: Asst. Prof. Sejal Jadav
ļ± Class declaration
ā¢ Following is the declaration for java.awt.event.KeyAdapter class:
public abstract class KeyAdapter
extendsObject
implements KeyListener
ā¢ Example: KeyAdapterExample.java
29. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Event Classes Listener Interfaces
ActionEvent ActionListener
ItemEvent ItemListener
FocusEvent FocusListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
TextEvent TextListener
KeyEvent KeyListener
30. Prepared By: Asst. Prof. Sejal Jadav
Q-11 Explain MouseEvent Class.
ā¢ There are seven types of mouse events.
ā¢ The MouseEvent class defines the following integer constants that can be
used to identify them:
MOUSE_CLICKED
ā¢ The user clicked the mouse.
MOUSE_DRAGGED
ā¢ The user dragged the mouse.
31. Prepared By: Asst. Prof. Sejal Jadav
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.
32. Prepared By: Asst. Prof. Sejal Jadav
ā¢ MouseEvent is a subclass of InputEvent.
Here is one of its constructors.
MouseEvent(Component src, int type, long when, int modifiers, int x, int
y, int clickCount, boolean opensPopup)
33. Prepared By: Asst. Prof. Sejal Jadav
MouseEvent(Component src, int type, long when, int
modifiers, int x, int y, int clickCount, boolean opensPopup)
ā¢ Here, src is a reference to the component that generated the event.
ā¢ The type is the type of the event and time specifies the system time at which the event was
generated.
ā¢ The modifiers parameter specifies any modifier keys that were pressed during the event.
ā¢ The x and y are the coordinates of the mouse location.
ā¢ The clickCount stores the number of mouse click.
ā¢ When opensPopup is true, it specifies that any popup menu was opened after the mouse
event.
34. Prepared By: Asst. Prof. Sejal Jadav
ā¢ The commonly used methods of MouseEvent are:
int getX( )
ā¢ It returns the x coordinate of the mouse cursor.
int getY( )
ā¢ It returns the y coordinate of the mouse cursor.
35. Prepared By: Asst. Prof. Sejal Jadav
void translatePoint(int x, int y)
ā¢ It translate the mouse pointer to specified x and y location.
int getClickCount( )
ā¢ It returns the number of mouse pointer clicked. For example 2 for double
click.
boolean isPopupTrigger( )
ā¢ It returns the true if any popup was opened after the mouse event.
36. Prepared By: Asst. Prof. Sejal Jadav
Q: 15 ) The MouseListener Interface:
ā¢ This interface defines five methods which are called when a mouse event is
generated These methods are:
void mouseClicked(MouseEvent me)
ā¢ This method is called when the mouse button is clicked.
void mouseEntered(MouseEvent me)
ā¢ This method is called when the mouse button is entered into a component.
37. Prepared By: Asst. Prof. Sejal Jadav
void mouseExited(MouseEvent me)
ā¢ This method is called when the mouse button is exited from a component.
void mousePressed(MouseEvent me)
ā¢ This method is called when the mouse button is pressed.
void mouseReleased(MouseEvent me)
ā¢ This method is called when the mouse button is released.
38. Prepared By: Asst. Prof. Sejal Jadav
Q: 15) The MouseMotionListener Interface:
ā¢ This interface defines two methods.
void mouseDragged(MouseEvent me)
ā¢ This method is called the mouse is dragged.
void mouseMoved(MouseEvent me)
ā¢ This method is called when mouse pointer is moved.
39. Prepared By: Asst. Prof. Sejal Jadav
Q-15) The MouseWheelListener Interface:
ā¢ It has one method:
void mouseWheelMoved(MouseWheelEvent mwe)
ā¢ This method is called when the wheel of the mouse is moved.
ā¢ Example: Q-16 Mouse Handling Program:
ā¢ MouseEvents.java
40. Prepared By: Asst. Prof. Sejal Jadav
Q: Java MouseAdapter Example [page: 23]
ā¢ The class MouseAdapter is an abstract (adapter) class for receiving
mouse events.
ā¢ All methods of this class are empty. This class is convenience class for
creating listener objects.
41. Prepared By: Asst. Prof. Sejal Jadav
ā¢ Class declaration:
ā¢ Following is the declaration for java.awt.event.MouseAdapter class:
Public abstract class MouseAdapter extends Object
implements
MouseListener,MouseWheelListener,MouseMotionListener
42. Prepared By: Asst. Prof. Sejal Jadav
ā¢ Example: MouseAdapterExample.java
43. Prepared By: Asst. Prof. Sejal Jadav
Q: Java MouseMotionAdapter Example [page:23]
ā¢ The class MouseMotionAdapter is an abstract (adapter) class for
receiving mouse motion events.
ā¢ All methods of this class are empty. This class is convenience class for
creating listener objects.
44. Prepared By: Asst. Prof. Sejal Jadav
ļ¶ Class Declaration
ā¢ Following is the declaration for java.awt.event.MouseMotionAdapter
class ā
public abstract class MouseMotionAdapter
extends Object
implements MouseMotionListener
ā¢ Example:MouseMotionAdapterExample.java
45. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Event Classes Listener Interfaces
ActionEvent ActionListener
ItemEvent ItemListener
FocusEvent FocusListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
TextEvent TextListener
KeyEvent KeyListener
46. Prepared By: Asst. Prof. Sejal Jadav
Q-12 Explain TextEvent Class.
ā¢ The TextEvent is generated when user inputs some text into a text field or a
text area. It has one integer constant.
TEXT_VALUE_CHANGED
ā¢ It is set when the text in a text field or a text area changed.
ā¢ Its constructor is: TextEvent(Object source,int type)
ā¢ Here source is the object that generated this event.
ā¢ The type is the type event which can be TEXT_VALUE_CHANGED.
47. Prepared By: Asst. Prof. Sejal Jadav
Q:15) The TextListener Interface:
ā¢ This interface ha one method:
void textChanged(TextEvent te)
ā¢ It is called when the text of a text area or text field is changed.
ā¢ Example: JavaExampleTextEvent.java
48. Prepared By: Asst. Prof. Sejal Jadav
Q-13 Explain WindowEvent Class.
ā¢ The WindowEvent class handles the events related to a window and it is
a subclass of ComponentEvent class.
ā¢ There are ten types of window events constant:
WINDOW_ACTIVATED
ā¢ The window was activated.
49. Prepared By: Asst. Prof. Sejal Jadav
WINDOW_DEACTIVATED
ā¢ The window was deactivated.
WINDOW_CLOSED
ā¢ The window has been closed.
WINDOW_CLOSING
ā¢ The user requested that the window be closed.
50. Prepared By: Asst. Prof. Sejal Jadav
WINDOW_OPENED
ā¢ The window was opened.
WINDOW_GAINED_FOCUS
ā¢ The window gained input focus.
WINDOW_LOST_FOCUS
ā¢ The window lost input focus.
51. Prepared By: Asst. Prof. Sejal Jadav
WINDOW_ICONIFIED
ā¢ The window was iconified. Ex it is minimized
WINDOW_DEICONIFIED
ā¢ The window was deiconified. Ex it is restored
WINDOW_STATE_CHANGED
ā¢ It is set when the state of the window is changed.
52. Prepared By: Asst. Prof. Sejal Jadav
ā¢ The constructors of WindowEvent are:
ā¢ WindowEvent(Window source, int type)
ā¢ WindowEvent(Window source, int type, Window other)
ā¢ WindowEvent(Window source, int type, int oldState, int newState)
ā¢ WindowEvent(Window source, int type, Window other, int oldState, int
newState)
53. Prepared By: Asst. Prof. Sejal Jadav
ā¢ Here, source is the window object that generated the window event.
ā¢ The type specifies the type of event such as WINDOW_CLOSING etc.
ā¢ The other specifies the opposite window when a focus event occurs.
ā¢ The oldState is the previous state of the event.
ā¢ The newState is the new state of the window.
54. Prepared By: Asst. Prof. Sejal Jadav
ļ¶ The methods of WindowEvent are:
Window getWindow( )
ā¢ It returns the Window object that generated window the event.
Window getOppositeWindow()
ā¢ It returns the opposite window that a focus event has been generated.
55. Prepared By: Asst. Prof. Sejal Jadav
int getOldState()
ā¢ It returns the previous state of the window.
int getNewState()
ā¢ It returns the new state of the window.
56. Prepared By: Asst. Prof. Sejal Jadav
Q:15) The WindowListener Interface:
ā¢ This interface has seven methods.
void windowActivated(WindowEvent we)
ā¢ This method is called when the window is activated.
void windowDeactivated(WindowEvent we)
ā¢ This method is called when the window is deactivated.
57. Prepared By: Asst. Prof. Sejal Jadav
void windowClosed(WindowEvent we)
ā¢ This method is called when the window has been closed.
void windowClosing(WindowEvent we)
ā¢ This method is called when the window is being closed.
void windowOpened(WindowEvent we)
ā¢ This method is called when the window has been opened.
58. Prepared By: Asst. Prof. Sejal Jadav
void windowIconified(WindowEvent we)
ā¢ This method is called when the window is minimized.
void windowDeiconified(WindowEvent we)
ā¢ This method is called when the window is restored.
ā¢ Example: WindowExample.java
59. Prepared By: Asst. Prof. Sejal Jadav
Q: 15) The WindowFocusListener Interface:
void windowGainedFocus(WindowEvent we)
ā¢ This is called when a window gains the input focus.
void windowLostFocus(WindowEvent we)
ā¢ This is called when a window loses the input focus.
ā¢ Example: WindowFocusListenerEx.java
60. Prepared By: Asst. Prof. Sejal Jadav
Q: Java WindowAdapter Example [page-22]
ā¢ The class WindowAdapter is an abstract (adapter) class for receiving
window events.
ā¢ All methods of this class are empty. This class is convenience class for
creating listener objects.
61. Prepared By: Asst. Prof. Sejal Jadav
ļ¶ Class declaration
ā¢ Following is the declaration for java.awt.event.WindowAdapter class:
Public abstract class WindowAdapter
Extends Object
Implements
WindowListener,WindowStateListener,WindowFocusListener
62. Prepared By: Asst. Prof. Sejal Jadav
ā¢ Dispose(): method :JFrame window to be destroyed and cleaned up by
the operating system.
ā¢ Example: AdapterExample1.java
63. Prepared By: Asst. Prof. Sejal Jadav
Q-14 Explain Source of Events.
ā¢ Source of events are:
Button
ā¢ It generates action events (ActionEvent class) when the button is pressed.
Checkbox
ā¢ It generates item events (ItemEvent class) when the check box is checked or
unchecked.
64. Prepared By: Asst. Prof. Sejal Jadav
Choice
ā¢ It generates item events (ItemEvent class) when the item is changed in the
choice.
List
ā¢ It can generateaction events as well as item events.When an item is double-
clicked, it generates ActionEvent and it generates ItemEvent when an item is
selected or deselected in the list.
65. Prepared By: Asst. Prof. Sejal Jadav
Menu Item
ā¢ It can generate action events as well as item events. It generates action events
when an menu item is selected from a menu and generates an item event
when a checkable menu item is selected or deselected.
Scrollbar
ā¢ It generates adjustment events (AdjustmentEvent class) when the scroll bar
is adjusted.
66. Prepared By: Asst. Prof. Sejal Jadav
Text controls such as text field, text area
ā¢ It generates text events when the user enters a character into text field
or text area.
Window
ā¢ It generates window events when a window is activated, opened, closed
etc.
67. Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
112