Handling GUI Generated Events 1

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.

Event Sources:-

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.

Event Handlers:-

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.

Delegation 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.

GUI Events

A Listener Example:-

The code given below shows the code for a simple JFrame with a single JButton on it.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class TestButton
  4. {
  5. private JFrame f;
  6. private JButton b;
  7. public TestButton()
  8. {
  9. f = new JFrame(“Test”);
  10. b = new JButton(“Press Me!”);
  11. b.setActionCommand(“ButtonPressed”);
  12. }
  13. public void launchFrame()
  14. {
  15. b.addActionListener(new ButtonHandler());
  16. f.add(b,BorderLayout.Center);
  17. f.pack();
  18. f.setVisible(true);
  19. }
  20. public static void main(String[] args)
  21. {
  22. TestButton guiApp = new TestButton();
  23. guiApp.launchFrame();
  24. }
  25. }

The ButtonHandler class, shown in the code given below, is that handler class to which the event is delegated.

  1. import java.awt.event.*:
  2. public class ButtonHandler implements ActionListener
  3. {
  4. public void actionPerformed(ActionEvent e)
  5. {
  6. System.out.println(“Action occurred”);
  7. System.out.println(“Button’s command is: ” + e.getActionCommand());
  8. }
  9. }

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.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply