Government Polytechnic Chh.
Sambhajinagar
Department Of Computer Engineering
-With Mr. G.U.Jadhav
Lecturer in Computer Engineering
import java.awt.event*
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Use delegation event model to develop event driven program for the
given problem.
2. Use relevant AWT/swing component (s) to handle the given event.
3. Use Adapter classes in java program to solve the given problem.
4. Use inner classes in java program to solve the given problem.
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Concept:
- A source generates an event and sends it to one or more listeners. In this scheme, the listener
simply 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 cleanly 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.
- In the delegation event model, listeners must register with a source in
order to receive an event notification.
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Event is an object that describes a state change in a source.
2. Some of the activities that cause events to be generated are pressing a button, entering a
character via the keyboard, selecting an item in a list, and clicking the mouse.
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. A source is an object that generates an event. This occurs when the internal state of that
object changes in some way.
2. Sources may generate more than one type of event.
3. A source must register listeners in order for the listeners to receive notifications about a
specific type of event.
4. Each type of event has its own registration method.
5. General form is :
public void addTypeListener(TypeListener el)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. A listener is an object that is notified when an event occurs.
2. It has two major requirements.
3. First, it must have been registered with one or more sources to receive notifications about
specific types of events.
4. Second, it must implement methods to receive and process these notifications.
5. The methods that receive and process events are defined in a set of interfaces found in
java.awt.event.
6. For example, the MouseMotionListener interface defines two methods to receive
notifications when the mouse is dragged or moved.
7. Any object may receive and process one or both of these events if it provides an
implementation of this interface.
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. The classes that represent events are at the core of Java’s event handling
mechanism.
2. At the root of the Java event class hierarchy is EventObject, which is in
java.util.
3. It is the superclass for all events.
4. EventObject contains two methods: Object getSource( ) and
String toString( ).
5. The class AWTEvent, defined within the java.awt package, is a subclass of
EventObject.
6. The package java.awt.event defines several types of events that are
generated by various user interface elements.
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Event Classes Description
ActionEvent Generated when a button is pressed, a list item is doubleclicked,
or a menu item is selected.
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.
KeyEvent Generated when input is received from the keyboard.
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
TextEvent Generated when the value of a text area or text field is
changed.
AdjustmentEvent Generated when a scroll bar is manipulated.
WindowEvent Generated when a window is activated, closed, deactivated,
deiconified, iconified, opened, or quit
ComponentEvent Generated when a component is hidden, moved, resized, or
becomes visible
Mr. Gajanan Jadhav
ContainerEvent Generated when a component is added to or removed from a
Lecturer In Computer
container
Engineering
Government Polytechnic, Chh.
Sambhajinagar
FocusEvent Generated when a component gains or loses keyboard focus.
Event Description
ActionEvent Generated when a button is pressed, a list item is doubleclicked,
or a menu item is selected.
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.
KeyEvent Generated when input is received from the keyboard.
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
TextEvent Generated when the value of a text area or text field is
changed.
AdjustmentEvent Generated when a scroll bar is manipulated.
WindowEvent Generated when a window is activated, closed, deactivated,
deiconified, iconified, opened, or quit
ComponentEvent Generated when a component is hidden, moved, resized, or
becomes visible
Mr. Gajanan Jadhav
ContainerEvent Generated when a component is added to or removed from a
Lecturer In Computer
container
Engineering
Government Polytechnic, Chh.
Sambhajinagar
FocusEvent Generated when a component gains or loses keyboard focus.
1. An ActionEvent is generated when a button is pressed, a list item is
double-clicked, or a menu item is selected.
2. Methods in Class :
a. String getActionCommand( )
b. int getModifiers( )
c. long getWhen( )
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1 This interface defines the actionPerformed( ) method that is invoked
when an action event occurs.
2. Methods in interface :
a. void actionPerformed(ActionEvent ae)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Three ways to implement event handler
• Within the class (Using this as an event handler)
• Outside the class (Using a separate event handler class)
• Anonymous class (Using an inline event handler)
Example
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. There are eight types of mouse events.
2. 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.
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
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Methods:
1. int getX( )
2. int getY( )
3. int getClickCount( )
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. This interface defines five methods.
2. If the mouse is pressed and released at the same point, mouseClicked( ) is
invoked.
3. When the mouse enters a component, the mouseEntered( ) method is
called.
4. When it leaves, mouseExited( ) is called.
5. The mousePressed( ) and mouseReleased( ) methods are invoked when the
mouse is pressed and released, respectively.
2. Methods in interface :
a. void actionPerformed(ActionEvent ae)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Methods in interface :
a. void mouseClicked(MouseEvent me)
b. void mouseEntered(MouseEvent me)
c. void mouseExited(MouseEvent me)
d. void mousePressed(MouseEvent me)
e. void mouseReleased(MouseEvent me)
1. Methods in Interface:
a. void mouseDragged(MouseEvent e)
b. void mouseMoved(MouseEvent e)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. An ItemEvent is generated when a check box or a list item is clicked or
when a checkable menu item is selected or deselected.
2. 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.
ITEM_STATE_CHANGED
2. Methods :
Object getItem( )
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. This interface defines the itemStateChanged( ) method that is invoked
when the state of an item changes.
2. Methods in interface :
void itemStateChanged(ItemEvent ie)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. WindowEvent is a subclass of ComponentEvent.
2. There are ten types of window events
3. The WindowEvent class defines integer constants that can be used to
identify them :
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.
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
Methods:
1. Window getWindow( )
2. Window getOppositeWindow()
3. int getOldState()
4. int getNewState()
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Methods in interface :
a. void windowActivated(WindowEvent we)
b. void windowClosed(WindowEvent we)
c. void windowClosing(WindowEvent we)
d. void windowDeactivated(WindowEvent we)
e. void windowDeiconified(WindowEvent we)
f. void windowIconified(WindowEvent we)
g. void windowOpened(WindowEvent we)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Methods in interface :
a. void windowGainedFocus(WindowEvent we)
b. void windowLostFocus(WindowEvent we)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. A KeyEvent is generated when keyboard input occurs
2. There are three types of key events, which are identified by these
integer constants:
KEY_PRESSED, KEY_RELEASED, and KEY_TYPED
3. The first two events are generated when any key is pressed or released.
4. The last event occurs only when a character is generated.
5. There are many other integer constants that are defined by KeyEvent.
For example, VK_0 through VK_9 and VK_A through VK_Z.
6. Here are some others:
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
Methods:
1. char getKeyChar( )
2. int getKeyCode( )
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Methods in interface :
a. Void keyPressed(KeyEvent ke)
b. void kayReleased(KeyEvent ke)
c. Void keyTyped(KeyEvent ke)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. An AdjustmentEvent is generated by a scroll bar
2. There are five types of adjustment events, which are identified by these
integer constants:
BLOCK_DECREMENT, BLOCK_INCREMENT, TRACK,
UNIT_DECREMENT, UNIT_INCREMENT
Methods:
1. int getAdjustmentType( )
2. int getValue( )
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Methods in interface :
a. void adjustmentValueChanged(AdjustmentEvent we)
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. Adapter classes are classes that have already implemented the various
interfaces available.
2. Each method of an interface is implemented as an empty method without
any code in an adapter class, and all you need to do is override the
method or methods you want to provide code for.
Adapter class Listener interface
WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar
1. A WindowAdapter is a convenience class that
implements the WindowListener interface .
2. The WindowListener interface has seven methods, but
most of the time, we only need to handle a few (such as
windowClosing).
3. Instead of implementing all methods, we can extend
WindowAdapter and override only the required method
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic, Chh.
Sambhajinagar