Before understanding event Listener in java. Firstly, we need to understand events. In java, an event is an object which specifies the change of state in the source. It occurs whenever an action takes place. For example clicking on a button, moving the mouse so on.
Types of event
The events can be broadly classified into two categories as below:
- Foreground Events – Those events require the direct interaction of the 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 the keyboard
- Selecting an item from the list
- Scrolling the page, etc.
- Background Events – Those events that require the interaction of the end-user are known as background events. Operating system interrupts, hardware or software failure, the timer expires, an operation completion is the example of background events.
Event Listener
The Event listener represents the interface that handles events. Every event class is associated with an event listener interface. Event listener interface defines one or more methods referred to as handlers. Event listeners implement various interfaces. Most of the event classes and their corresponding listener interfaces, defined in java.awt.event package.
Class declaration
Following is the declaration for java.util.EventListener interface:
public interface EventListener
Here are some common event listener interfaces of java.awt.event package. As below
The ActionListener interface:
This interface defines a method to respond to events. When an action event occurs, the actionPerformed() method can specify by the ActionListener interface.
Generally, actionPerformed() method is
void actionPreformed(ActionEvent e)
Where, e is the reference to an object of ActionEvent class.
The AdjustmentListener Interface:
This interface defines a adjustmentValueChanged() method to respond to an event that occurs when an adjustable object.
Generally, adjustmentValueChanged() method is
void AdjustmentValueChanged(AdjustmentEvent e)
Where, e is the reference to an object of AdjustmentEvent class.
ItemListener Interface:
This interface defines itemStateChanged() method to respond to events that occur when a component is selected or deselected.
The general form of the itemStateChanged() method is,
void itemStateChanged(ItemEvent e)
KeyListener Interface:
This interface defines keypressed() method that originates when an input is received from the keyboard.
The general form of the method defines by KeyListener is
void keyPressed(keyEvent key)
MouseListener interface:
This interface defines the method to respond to events arising when the mouse cursor moved into or when we press the mouse button.
the method defined by MouseListener interface.
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
MouseMotionListener Interface.
This interface defines the method called when the mouse is moved or dragged with a button pressed.
method defined by MouseMotionListener is
void mouseMoved(MouseEvent me)
void mouseDragged(MouseEvent me)
Text Listener interface
This interface defines the method that called when the value of the text textfield or textarea in changed.
method defined by TextListener is
void tectValueChanged(TextEvent te)
The Interfaces with their corresponding Event class as shown in the following table below.
Interface | Corresponding Event class |
---|---|
ActionListener | ActionEvent |
AdjustmentListener | AdjustmentEvent |
FocusListener | FocusEvent |
ItemListener | ItemEvent |
KeyListener | KeyEvent |
MouseListener | MouseEvent |
MouseMotionListener | MouseEvent |
TextListener | TextEvent |
hey, guys hope you enjoyed this article, if you want java or other language tutorials then, please visit my friend’s site Technicalblog, She has explained every topic very descriptive ways.
Recommended Post:
Convert java object into a JSON
Difference between the Java and JavaScript
How are java objects stored in memory?
Reference:
Book// learn programming in java/Anurag Gupta, Dinesh sharma, Anshuman Shrama.
Helpful post