Event Classes and Event Listener Interfaces

Event Classes
There are many types of events that are generated by your AWT Application. These events are used to make the application more effective and efficient. Generally, there are twelve types of event are used in Java AWT.
When the user interacts with a GUI application, an event is generated. Examples of user events are clicking a button, selecting an item or closing a window. Events are represented as Objects in JavaTM technology. The super class of all event classes is java.util.EventObject. Some of the important classes and their hierarchy is shown below.

These are twelve mentioned events are explained as follows :-
1. ActionEvent: This is the ActionEvent class extends from the AWTEvent class. It indicates the component-defined events occurred i.e. the event generated by the component like Button, Checkboxes etc. The generated event is passed to every EventListener objects that receives such types of events using the addActionListener() method of the object.
2. AdjustmentEvent: This is the AdjustmentEvent class extends from the AWTEvent class. When the Adjustable Value is changed then the event is generated.
3. ComponentEvent: ComponentEvent class also extends from the AWTEvent class. This class creates the low-level event which indicates if the object moved, changed and it's states (visibility of the object). This class only performs the notification about the state of the object. The ComponentEvent class performs like root class for other component-level events.
4. ContainerEvent: The ContainerEvent class extends from the ComponentEvent class. This is a low-level event which is generated when container's contents changes because of addition or removal of a components.
5. FocusEvent: The FocusEvent class also extends from the ComponentEvent class. This class indicates about the focus where the focus has gained or lost by the object. The generated event is passed to every objects that is registered to receive such type of events using the addFocusListener() method of the object.
6. InputEvent: The InputEvent class also extends from the ComponentEvent class. This event class handles all the component-level input events. This class acts as a root class for all component-level input events.
7. ItemEvent: The ItemEvent class extends from the AWTEvent class. The ItemEvent class handles all the indication about the selection of the object i.e. whether selected or not. The generated event is passed to every ItemListener objects that is registered to receive such types of event using the addItemListener() method of the object.
8. KeyEvent: KeyEvent class extends from the InputEvent class. The KeyEvent class handles all the indication related to the key operation in the application if you press any key for any purposes of the object then the generated event gives the information about the pressed key. This type of events check whether the pressed key left key or right key, 'A' or 'a' etc.
9. MouseEvent: MouseEvent class also extends from the InputEvent class. The MouseEvent class handle all events generated during the mouse operation for the object. That contains the information whether mouse is clicked or not if clicked then checks the pressed key is left or right.
10. PaintEvent: PaintEvent class also extends from the ComponentEvent class. The PaintEvent class only ensures that the paint() or update() are serialized along with the other events delivered from the event queue.
11. TextEvent: TextEvent class extends from the AWTEvent class. TextEvent is generated when the text of the object is changed. The generated events are passed to every TextListener object which is registered to receive such type of events using the addTextListener() method of the object.
12. WindowEvent : WindowEvent class extends from the ComponentEvent class. If the window or the frame of your application is changed (Opened, closed, activated, deactivated or any other events are generated), WindowEvent is generated.
Event types and corresponding EventSource & EventListener
Event Type Event Source Event Listener interface
ActionEvent Button, List, MenuItem, TextField ActionListener
AdjustmentEvent Scrollbar AdjustmentListener
ItemEvent Choice, Checkbox, CheckboxMenuItem, List ItemListener
TextEvent TextArea, TextField TextListener
ComponentEvent Component ComponentListener
ContainerEvent Container ContainerListener
FocusEvent Component FocusListener
KeyEvent Component KeyListener
MouseEvent Component MouseListener, MouseMotionListener
WindowEvent Window WindowListener

Event Listener Interfaces and corresponding methods which it defines
Event Listener interface Event Listener Methods
ActionListener actionPerformed(ActionEvent evt)
AdjustmentListener adjustmentValueChanged(AjustmentEvent evt)
ItemListener itemStateChanged(ItemEvent evt)
TextListener textValueChanged(TextEvent evt)
ComponentListener componentHidden(ComponentEvent evt), componentMoved(ComponentEvent evt), componentResized(ComponentEvent evt), componentShown(ComponentEvent evt)
ContainerListener componentAdded(ContainerEvent evt), componentRemoved(ContainerEvent evt)
FocusListener focusGained(FocusEvent evt), focusLost(FocusEvent evt)
KeyListener keyPressed(KeyEvent evt), keyReleased(KeyEvent evt), keyTyped(KeyEvent evt)
MouseListener mouseClicked(MouseEvent evt), mouseEntered(MouseEvent evt), mouseExited(MouseEvent evt), mousePressed(MouseEvent evt), mouseReleased(MouseEvent evt)
MouseMotionListener mouseDragged(MouseEvent evt), mouseMoved(MouseEvent evt)
WindowListener windowActivated(WindowEvent evt), windowClosed(WindowEvent evt), windowClosing(WindowEvent evt), windowDeactivated(WindowEvent evt), windowDeiconified(WindowEvent evt), windowIconified(WindowEvent evt), windowOpened(WindowEvent evt)

Event Listener Interfaces
The EventListener interface is the primary method for handling events. Users implement the EventListener interface and register their listener on an EventTarget using the AddEventListener method. The users should also remove their EventListener from its EventTarget after they have completed using the listener.
When a Node is copied using the cloneNode method the EventListeners attached to the source Node are not attached to the copied Node. If the user wishes the same EventListeners to be added to the newly created copy the user must add them manually.
An event listener registers with an event source to receive notifications about the events of a particular type. Various event listener interfaces defined in the java.awt.event package are given below :.
Interface         Description
ActionListener Defines the actionPerformed() method
to receive and process action events.
MouseListener Defines five methods to receive mouse
events, such as when a mouse is
clicked, pressed, released, enters, or
exits a component
MouseMotionListener Defines two methods to receive
events, such as when a mouse is
dragged or moved.
AdjustmentListner Defines the adjustmentValueChanged()
method to receive and process the
adjustment events.
TextListener Defines the textValueChanged()
method to receive and process an
event when the text value changes.
WindowListener Defines seven window methods to
receive events.
ItemListener Defines the itemStateChanged()
method when an item has been
selected or deselected by the user.
.
The following code shows the generation of an action event and its handling by the ActionListener interface :
Example :-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ActionListenerDemo extends JFrame implements ActionListener {
JButton b1, b2;
JLabel lbl;
int clicked;
final String str = " Number of times button clicked = ";
public ActionListenerDemo(String strName) {
super(strName);
getContentPane().setLayout(new FlowLayout());
JPanel p = new JPanel();
b1 = new JButton("Click Me");
b2 = new JButton("Quit");
p.setLayout(new FlowLayout());
p.add(b1);
p.add(b2);
lbl = new JLabel(str + clicked, JLabel.CENTER);
getContentPane().add(p);
getContentPane().add(lbl);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if ("Click Me".equals(s)) {
clicked++;
lbl.setText(str + clicked);
}
if ("Quit".equals(s)) {
System.exit(0);
}
}
public static void main(String args[]) {
ActionListenerDemo t = new ActionListenerDemo("Action Events");
t.setSize(300, 300);
t.setVisible(true);
}
}
Output: -
C:\java\awt>javac ActionListenerDemo.java
C:\java\awt>java ActionListenerDemo


Free Web Hosting