Unit Iii
Unit Iii
Event Handling
What is an Event?
Change in the state of an object is known as event i.e. event describes the change
in state of source. Events are generated as result of user interaction with the
graphical user interface components. For example, clicking on a button, moving the
mouse, entering a character through keyboard,selecting an item from list, scrolling
the page are the activities that causes an event to happen.
Types of Event
The events can be broadly classified into two categories:
Foreground Events - Those events which require the direct interaction of user.They are
generated as consequences of a person interacting with the graphical components in
Graphical User Interface. For example, clicking on a button, moving the mouse, entering
a character through keyboard,selecting an item from list, scrolling the page etc.
Background Events - Those events that require the interaction of end user are known
as background events. Operating system interrupts, hardware or software failure, timer
expires, an operation completion are the example of background events.
The benefit of this approach is that the user interface logic is completely separated
from the logic that generates the event. The user interface element is able to
delegate the processing of an event to the separate piece of code. In this model
,Listener needs to be registered with the source object so that the listener can
receive the event notification. This is an efficient way of handling the event because
the event notifications are sent only to those listener that want to receive them.
P a g e 1 | 24
UNIT – IIIEVENT HANDLING
Now the object of concerned event class is created automatically and information about
the source and the event get populated with in same object.
Event object is forwarded to the method of registered listener class.
the method is now get executed and returns.
Event Classes
ActionEvent Class
This class is defined in java.awt.event package. The ActionEvent is generated when
button is clicked or the item of a list is double clicked.
Class methods
S.N. Method & Description
1 java.lang.String getActionCommand()
Returns the command string associated with this action.
2 int getModifiers()
Returns the modifier keys held down during this action event.
3 long getWhen()
Returns the timestamp of when this event occurred.
4 java.lang.String paramString()
Returns a parameter string identifying this action event.
KeyEvent Class
On entering the character the Key event is generated. There are three types of key
events which are represented by the integer constants. These key events are
following
KEY_PRESSED
KEY_RELASED
KEY_TYPED
Class methods
S.N. Method & Description
1 char getKeyChar()
Returns the character associated with the key in this event.
2 int getKeyCode()
Returns the integer keyCode associated with the key in this event.
P a g e 2 | 24
UNIT – IIIEVENT HANDLING
3 int getKeyLocation()
Returns the location of the key that originated this key event.
6 boolean isActionKey()
Returns whether the key in this event is an "action" key.
7 String paramString()
Returns a parameter string identifying this event.
MouseEvent Class
This event indicates a mouse action occurred in a component. This low-level event
is generated by a component object for Mouse Events and Mouse motion events.
a mouse button is pressed
a mouse button is released
a mouse button is clicked (pressed and released)
a mouse cursor enters the unobscured part of component's geometry
a mouse cursor exits the unobscured part of component's geometry
a mouse is moved
the mouse is dragged
Class methods
S.N. Method & Description
1 int getButton()
Returns which, if any, of the mouse buttons has changed state.
2 int getClickCount()
Returns the number of mouse clicks associated with this event.
3 Point getLocationOnScreen()
Returns the absolute x, y position of the event.
P a g e 3 | 24
UNIT – IIIEVENT HANDLING
Returns a String describing the modifier keys and mouse buttons that were
down during the event, such as "Shift", or "Ctrl+Shift".
5 Point getPoint()
Returns the x,y position of the event relative to the source component.
6 int getX()
Returns the horizontal x position of the event relative to the source
component.
7 int getXOnScreen()
Returns the absolute horizontal x position of the event.
8 int getY()
Returns the vertical y position of the event relative to the source component.
9 int getYOnScreen()
Returns the absolute vertical y position of the event.
11 String paramString()
Returns a parameter string identifying this event.
TextEvent Class
The object of this class represents the text events.The TextEvent is generated when
character is entered in the text fields or text area. The TextEvent instance does not
include the characters currently in the text component that generated the event rather
we are provided with other methods to retrieve that information.
Class methods
S.N. Method & Description
1 String paramString()
Returns a parameter string identifying this text event.
P a g e 4 | 24
UNIT – IIIEVENT HANDLING
WindowEvent Class
The object of this class represents the change in state of a window.This low-level event
is generated by a Window object when it is opened, closed, activated, deactivated,
iconified, or deiconified, or when focus is transfered into or out of the Window.
Class methods
S.N. Method & Description
1 int getNewState()
For WINDOW_STATE_CHANGED events returns the new state of the
window.
2 int getOldState()
For WINDOW_STATE_CHANGED events returns the previous state of the
window.
3 Window getOppositeWindow()
Returns the other Window involved in this focus or activation change.
4 Window getWindow()
Returns the originator of the event.
5 String paramString()
Returns a parameter string identifying this event.
Event Listeners
Event Listeners Interface
It is a marker interface which every listener interface has to extend.This class is
defined in java.util package.
1. ActionListener Interface
2. ItemListener Interface
3. KeyListener Interface
4. MouseListener Interface
5. MouseMotionListener Interface
6. TextListener Interface
7. WindowListener Interface
P a g e 5 | 24
UNIT – IIIEVENT HANDLING
1. ActionListener Interface
The class which processes the ActionEvent should implement this interface.The
object of that class must be registered with a component. The object can be
registered using the addActionListener() method. When the action event occurs,
that object's actionPerformed method is invoked.
Interface declaration
Following is the declaration for java.awt.event.ActionListener interface:
public interface ActionListener
extends EventListener
Interface methods
S.N. Method & Description
1 void actionPerformed(ActionEvent e)
Invoked when an action occurs.
Methods inherited
This interface inherits methods from the following interfaces:
java.awt.EventListener
ActionListener Example
import java.awt.*;
import java.awt.event.*;
public AwtListenerDemo(){
prepareGUI();
}
P a g e 6 | 24
UNIT – IIIEVENT HANDLING
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
okButton.addActionListener(new CustomActionListener());
panel.add(okButton);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
Output :
P a g e 7 | 24
UNIT – IIIEVENT HANDLING
2. ItemListener Interface
The class which processes the ItemEvent should implement this interface.The
object of that class must be registered with a component. The object can be
registered using the addItemListener() method. When the action event
occurs, that object's itemStateChanged method is invoked.
Interface declaration
Following is the declaration for java.awt.event.ItemListener interface:
Interface methods
S.N. Method & Description
1 void itemStateChanged(ItemEvent e)
Invoked when an item has been selected or deselected by the user.
Methods inherited
This interface inherits methods from the following interfaces:
java.awt.EventListener
ItemListener Example
import java.awt.*;
import java.awt.event.*;
P a g e 8 | 24
UNIT – IIIEVENT HANDLING
public AwtListenerDemo(){
prepareGUI();
}
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
chkApple.addItemListener(new CustomItemListener());
chkMango.addItemListener(new CustomItemListener());
chkPeer.addItemListener(new CustomItemListener());
controlPanel.add(chkApple);
controlPanel.add(chkMango);
controlPanel.add(chkPeer);
mainFrame.setVisible(true);
}
P a g e 9 | 24
UNIT – IIIEVENT HANDLING
Output :
3. KeyListener Interface
The class which processes the KeyEvent should implement this interface.The
object of that class must be registered with a component. The object can be
registered using the addKeyListener() method.
Interface declaration
Following is the declaration for java.awt.event.KeyListener interface:
public interface KeyListener
extends EventListener
Interface methods
S.N. Method & Description
1 void keyPressed(KeyEvent e)
Invoked when a key has been pressed.
P a g e 10 | 24
UNIT – IIIEVENT HANDLING
2 void keyReleased(KeyEvent e)
Invoked when a key has been released.
3 void keyTyped(KeyEvent e)
Invoked when a key has been typed.
Methods inherited
This interface inherits methods from the following interfaces:
java.awt.EventListener
KeyListener Example
import java.awt.*;
import java.awt.event.*;
public AwtListenerDemo(){
prepareGUI();
}
mainFrame.add(headerLabel);
P a g e 11 | 24
UNIT – IIIEVENT HANDLING
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
textField.addKeyListener(new CustomKeyListener());
Button okButton = new Button("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("Entered text: " + textField.getText());
}
});
controlPanel.add(textField);
controlPanel.add(okButton);
mainFrame.setVisible(true);
}
Output :
P a g e 12 | 24
UNIT – IIIEVENT HANDLING
4. MouseListener Interface
The class which processes the MouseEvent should implement this
interface.The object of that class must be registered with a component. The
object can be registered using the addMouseListener() method.
Interface declaration
Following is the declaration for java.awt.event.MouseListener interface:
public interface MouseListener
extends EventListener
Interface methods
S.N. Method & Description
1 void mouseClicked(MouseEvent e)
Invoked when the mouse button has been clicked (pressed and released) on
a component.
2 void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.
3 void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.
4 void mousePressed(MouseEvent e)
P a g e 13 | 24
UNIT – IIIEVENT HANDLING
5 void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.
Methods inherited
This interface inherits methods from the following interfaces:
java.awt.EventListener
MouseListener Example
import java.awt.*;
import java.awt.event.*;
public AwtListenerDemo(){
prepareGUI();
}
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
P a g e 14 | 24
UNIT – IIIEVENT HANDLING
mainFrame.setVisible(true);
}
msglabel.addMouseListener(new CustomMouseListener());
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
class CustomMouseListener implements MouseListener{
Output :
P a g e 15 | 24
UNIT – IIIEVENT HANDLING
5. MouseMotionListener Interface
Introduction
The interfaceMouseMotionListener is used for receiving mouse motion events
on a component. The class that process mouse motion events needs to
implements this interface.
Class declaration
Following is the declaration
for java.awt.event.MouseMotionListenerinterface:
public interface MouseMotionListener
extends EventListener
Interface methods
S.N. Method & Description
1 void mouseDragged(MouseEvent e)
Invoked when a mouse button is pressed on a component and then dragged.
2 void mouseMoved(MouseEvent e)
Invoked when the mouse cursor has been moved onto a component but no
buttons have been pushed.
Methods inherited
This class inherits methods from the following interfaces:
java.awt.event.EventListener
P a g e 16 | 24
UNIT – IIIEVENT HANDLING
MouseMotionListener Example
import java.awt.*;
import java.awt.event.*;
public AwtListenerDemo(){
prepareGUI();
}
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
P a g e 17 | 24
UNIT – IIIEVENT HANDLING
msglabel.setAlignment(Label.CENTER);
msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
Output :
P a g e 18 | 24
UNIT – IIIEVENT HANDLING
6. TextListener Interface
The class which processes the TextEvent should implement this interface.The
object of that class must be registered with a component. The object can be
registered using the addTextListener() method.
Interface declaration
Following is the declaration for java.awt.event.TextListener interface:
extends EventListener
Interface methods
S.N. Method & Description
1 void textValueChanged(TextEvent e)
Invoked when the value of the text has changed.
Methods inherited
This interface inherits methods from the following interfaces:
java.awt.EventListener
TextListener Example
import java.awt.*;
import java.awt.event.*;
public AwtListenerDemo(){
prepareGUI();
}
P a g e 19 | 24
UNIT – IIIEVENT HANDLING
});
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
textField.addTextListener(new CustomTextListener());
Button okButton = new Button("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("Entered text: "
+ textField.getText());
}
});
controlPanel.add(textField);
controlPanel.add(okButton);
mainFrame.setVisible(true);
}
Output :
P a g e 20 | 24
UNIT – IIIEVENT HANDLING
7. WindowListener Interface
The class which processes the WindowEvent should implement this
interface.The object of that class must be registered with a component. The
object can be registered using the addWindowListener() method.
Interface declaration
Following is the declaration for java.awt.event.WindowListener interface:
public interface WindowListener
extends EventListener
Interface methods
S.N. Method & Description
1 void windowActivated(WindowEvent e)
Invoked when the Window is set to be the active Window.
2 void windowClosed(WindowEvent e)
Invoked when a window has been closed as the result of calling dispose on
the window.
3 void windowClosing(WindowEvent e)
Invoked when the user attempts to close the window from the window's
system menu.
P a g e 21 | 24
UNIT – IIIEVENT HANDLING
4 void windowDeactivated(WindowEvent e)
Invoked when a Window is no longer the active Window.
5 void windowDeiconified(WindowEvent e)
Invoked when a window is changed from a minimized to a normal state.
6 void windowIconified(WindowEvent e)
Invoked when a window is changed from a normal to a minimized state.
7 void windowOpened(WindowEvent e)
Invoked the first time a window is made visible.
Methods inherited
This interface inherits methods from the following interfaces:
java.awt.EventListener
WindowListener Example
import java.awt.*;
import java.awt.event.*;
public AwtListenerDemo(){
prepareGUI();
}
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
P a g e 23 | 24
UNIT – IIIEVENT HANDLING
Output :
P a g e 24 | 24