What is an Event?
- When the user performs an action at the user interface level (clicks a mouse or presses a key), this causes an event to be issued.
- Events are objects that describe what has happened. A number of different types of event classes exist to describe different categories of user action.
- When a user clicks a GUI button, an event object is created by the JVM and the button sends this event to the event handler for that button by calling the actionPerformed method.
An event source is the generator of an event. For example, a mouse click on a JButton component generates an ActionEvent instance with the button as the sourec. The ActionEvent instance with the information about an events that just took place. The ActionEvent contains:
- getActionCommand, which returns the command name associated with the action.
- getModifiers, which returns any modifiers held during the action.
- getWhen, which returns the timestamp when the event occurred.
- paramString, which returns a string identifying action and the associated command.
An event handler is the method that receives an event object, deciphers it, and processes the user’s interaction.
Java SE Event Model:-
This section describes the delegation event model.
- The delegation event model came into existence with JDK version 1.1. with this model, events are sent to the component from which the event originated, but it is up to each component to propagate the event handlers that receive and process the event.
- In this way, the event handler can be in an object separate from the component.
- Listeners are classes that implement the EventListener interface.
- Events are objects that are reported only to registered listeners. Every event has a corresponding listener interface that mandates which method must be defined in a class suited to receiving that type of event.
- A class which implements an interface defines those methods, and can be registered as a listener.
- The Events from components that have no registered listeners are not propagated.
A Listener Example:-
The code given below shows the code for a simple JFrame with a single JButton on it.
- import javax.swing.*;
- import java.awt.*;
- public class TestButton
- private JFrame f;
- private JButton b;
- public TestButton()
- f = new JFrame(“Test”);
- b = new JButton(“Press Me!”);
- public void launchFrame()
- b.addActionListener(new ButtonHandler());
- public static void main(String args)
- TestButton guiApp = new TestButton();
The ButtonHandler class, shown in the code given below, is that handler class to which the event is delegated.
- import java.awt.event.*:
- public class ButtonHandler implements ActionListener
- public void actionPerformed(ActionEvent e)
- System.out.println(“Action occurred”);
- System.out.println(“Button’s command is: ” + e.getActionCommand());
The code of ButtonHandler class has the following characteristics:
- The JButton class inherits the addActionListener(ActionListener) method from javax.swing.AbstractButton which is the superclass of JButton.
- An ActionListener interface defines a single method, actionPerformed, which receives an ActionEvent.
- After it is created, a JButton object can have an object registered as a listener for ActionEvents through the addActionListener() method. The registered listener is instantiated from a class that implements the ActionListener interface.
- When the JButton object is clicked, an ActionEvent is sent. The ActionEvent is received through the actionPerformed() method of any ActionListener that is registered on the button through its addActionListener() method.
- The method, getActionCommand(), of the ActionEvent class returns the command name associated with this action. On Line 13, the action aommand for this button is set to ButtonPressed.
Note: – There are other ways to determine why an event has been received. In particular, the method getSource(), documented in the java.util.EventObject base class, is often useful because it enables you to obtain the reference to the object that sent the event.
Events are not handled accidentally. The objects that are scheduled to listen to particular GUI component register themselves with that component.
- When an event occurs, only the objects that were registered receive a message that the event occurred.
- The delegation model is good for the distribution of work among objects.
Events do not have to be related to Swing components. This event model provides support for the JavaBeans architecture.