Java Event Handling (Part-1)

Discussion in 'Java' started by techgeek.in, Apr 25, 2010.

  1. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in

    Introduction



    Applets are event-driven programs. Event handling is at the core of successful applet programming. Most events to which an applet will respond are generated by the user. There are several types of events. The most commonly handled events are those generated by the mouse, the keyboard, and various controls, such as a push button. For example, when the user clicks on the mouse, the environment generates an event that it sends to the program. The program must then figure out what the mouse click means and act accordingly.

    Events are supported by the java.awt.event package. The way in which events are handled by an applet changed significantly between the original version of Java (1.0) and modern versions of Java, beginning with version 1.1. The 1.0 method of event handling is still supported, but it is not recommended for new programs. Also, many of the methods that support the old 1.0 event model have been deprecated.

    The Delegation Event Model



    The modern approach to handling events is based on the delegation event model. A source generates an event and sends it to one or more listeners. The listener simply waits until it receives an event. Once received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. Listeners must register with a source in order to receive an event notification. This provides an important benefit which is that notifications are sent only to listeners that want to receive them.

    Event



    An event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse, etc. Events may also occur that are not directly caused by interactions with a user interface. For example, an event may be generated when a timer expires, a counter exceeds a value, software or hardware failure occurs, or an operation is completed.

    Event Sources



    A source is an object that generates an event. This occurs when the internal state of that object changes in some way. Sources may generate more than one type of event. A source must register listeners in order for the listeners to receive notifications about a specific type of event. Each type of event has its own registration method.

    Following is the general form:

    public void addTypeListener (TypeListener el)

    Here, Type is the name of the event and el is a reference to the event listener. For example, the method that registers a keyboard event listener is called addKeyListener( ). The method that registers a mouse motion listener is called addMouseMotionListener().

    When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event. Notifications are sent only to the listeners that register to receive those events.

    Some event sources allow only one listener to register. The general form of such a method is as follows:

    public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException

    Here, Type is the name of the event and el is a reference to the event listener. When such an event occurs, the registered listener is notified. This is known as unicasting the event.

    A source also provides a method that allows a listener to unregister a listener in a specific type of event. The general form of such a method is as follows:

    public void removeTypeListener(TypeListener el)

    Here, Type is the name of the event and el is a reference to the event listener.

    For example, to remove a keyboard listener, you would call removeKeyListener( ).

    The methods that add or remove listeners are provided by the source that generates events. For example, the Component class provides methods to add and remove keyboard and mouse event listeners.

    Event Listeners



    A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications.

    The methods that receive and process events are defined in a set of interfaces found in

    java.awt.event.

    For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved. Any object may receive and process one or both of these events if it provides an implementation of this interface.

    Event Classes



    At the core of Java’s event handling mechanism are the classes that represent events. In this article I will introduce the various event classes.

    At the root of the Java event class hierarchy is EventObject, which is in java.util. It is the superclass for all events.

    Its one constructor is shown here:

    EventObject(Object src):- Here, src is the object that generates this event.

    EventObject contains two methods: getSource( ) and toString( ).

    The getSource( ) method returns the source of the event. Its general form is as follows:

    Object getSource( )

    toString( ) method returns the string equivalent of the event.

    AWTEvent is a class defined within the java.awt package. This class is a subclass of EventObject. It is the superclass (either directly or indirectly) of all AWT-based events are handled by the delegation event model.

    It has a getID( ) method that can be used to determine the type of the event. The prototype of this method is as follows:

    int getID( )

    The package java.awt.event defines several types of events that are generated by various user interface elements. Following are some of the important classes:-
    1. ActionEvent -- Generated when a button is pressed, a list item is double-clicked, or a menu item is selected.
    2. AdjustmentEvent -- Generated when a scroll bar is manipulated.
    3. ComponentEvent -- Generated when a component is hidden, moved, resized, or becomes visible.
    4. ContainerEvent -- Generated when a component is added to or removed from a container.
    5. FocusEvent -- Generated when a component gains or loses keyboard focus.
    6. InputEvent -- Abstract super class for all component input event classes.
    7. ItemEvent -- Generated when a check box or list item is clicked; also when a choice selection is made or a checkable menu item is selected or deselected.
    8. KeyEvent -- Generated when input is received from the keyboard.
    9. MouseEvent -- Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component.
    10. MouseWheelEvent -- Generated when the mouse wheel is moved.
    11. TextEvent -- Generated when the value of a text area or text field is changed.
    12. WindowEvent -- Generated when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

    Sources of Events



    Some of the event sources are as follows:
    1. Button -- Generates action events when the button is pressed.
    2. Checkbox -- Generates item events when the check box is selected or deselected.
    3. Choice -- Generates item events when the choice is changed.
    4. List -- Generates action events when an item is double-clicked; generates item events when an item is selected or deselected.
    5. Menu Item -- Generates action events when a menu item is selected; generates item events when a checkable menu item is selected or deselected.
    6. Scrollbar -- Generates adjustment events when the scroll bar is manipulated.
    7. Textcomponents -- Generates text events when the user enters a character.
    8. Window -- Generates window events when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

    Event Listener Interfaces



    Some of the Event listener interfaces are as follows:
    1. ActionListener -- Defines one method to receive action events.
    2. AdjustmentListener -- Defines one method to receive adjustment events.
    3. ComponentListener -- Defines four methods to recognize when a component is hidden, moved, resized, or shown.
    4. ContainerListener -- Defines two methods to recognize when a component is added to or removed from a container.
    5. FocusListener -- Defines two methods to recognize when a component gains or loses keyboard focus.
    6. ItemListener -- Defines one method to recognize when the state of an item changes.
    7. KeyListener -- Defines three methods to recognize when a key is pressed, released, or typed.
    8. MouseListener -- Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released.
    9. MouseMotionListener -- Defines two methods to recognize when the mouse is dragged or moved.
    10. MouseWheelListener -- Defines one method to recognize when the mouse wheel is moved.
    11. TextListener -- Defines one method to recognize when a text value changes.
    12. WindowFocusListener -- Defines two methods to recognize when a window gains or loses input focus.
    13. WindowListener -- Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice