Course Content‎ > ‎Session 7‎ > ‎

Event Handling

Events are objects that represent user-initiated actions. For example, clicking a mouse, pressing return key etc. AWT and Swing components both generate events. In addition to the AWT events, Swing events are also supported in Java. Sun Microsystems introduced the delegation-based event-handling mechanism into the Java libraries with the release of JDK 1.1 and JavaBeans. 

There are two types of AWT events: Semantic and Low-Level Events. 

Semantic Events  

Semantic Events have a different meaning, depending on who fires it. For example, ActionEvent (indicates a component defined action has occurred), ItemEvent (whether an item was selected or deselected; it is fired by JComboBox, JButton), TextEvent (the contents of a text field or text area were changed), AdjustmentEvent(e.g. the user adjusted the scrollbar). 

Low-Level Events  

Low-level events always have the same meaning; for example, ComponentEvent (the component was moved, shown or hidden), MouseEvent (the mouse button was depressed, released, moved or dragged), KeyEvent (a key was pressed or released), FocusEvent (a component gets focus or loses focus), WindowEvent (the window was activated, deactivated, iconified or closed), ContainerEvent (a component has been added or removed).

Event handling is common in AWT and Swing. 

Delegation-based-event handling 

There are extensive sets of class libraries in java.awt.event and javax.swing.event, used for event handling in Java. Event Sources have methods that allow you to register event listeners with them. Event Listeners are interfaces, used to support the dispatching of events. There can be multiple listeners for the same event type. Each Event Listener has one or more methods corresponding to the types of events. There can be an Adapter class that implements a Listener Interface with empty methods. When an event occurs to the source, the source sends a notification of that event to all the listener objects that were registered for that event. A listener object is implemented. A listener object is an instance of a class that implements a special interface called a listener interface. An event source object can register these listener objects and send them event objects.
    
The event source sends out event objects to all listeners, when that event occurs. There is no guarantee, which listener is notified first. The listener object then uses the information in the event object to determine the reaction to the event. Selection type events (ItemEvent) generate two events per user action (selection & deselection).

Each of the AWT listener interfaces that has more than one method comes with a companion Adapter class that implements all the methods in the interface, but does nothing with them. For example, the WindowAdapter class has seven methods. An interface such as ActionListener that has a single method does not need an Adapter class. Let’s take an example to make use of the WindowAdapter. We can extend the WindowAdapter class, inherit its methods and override the WindowClosing method:

    class Terminator extends WindowAdapter
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    }

Now, we can register an object of type Terminator as the event listener:
 
    class MyFrame extends JFrame
    {
        public MyFrame()
        {
            Terminator x= new Terminator();
            AddWindowListener(x);
            ...
        }
        ...
    }  

Now whenever the frame generates a window event, it passes it to x object by calling one of its seven methods. The windowClosing method calls System.exit(0), terminating the application. You can also simply write:
    
    class MyFrame extends JFrame
    {
        public MyFrame()
        {
            addWindowListener(new Terminator());
            ...
        }
        ...
    }  

Comments