Quick Contact





    When a normal Java program (which is not GUI based) needs input, it prompts the user and then calls some input method, such as readline() i.e. the interaction is initiated by the program. This is not the way in which gui-based programs behave. The user initiates interaction with the program rather than program initiating the action.

    For example, in a word processing software, user initiates action by clicking on different buttons, which generates an event and some piece of code is executed as a result and accordingly some action takes place. The clicking on save button will invoke the method which saves the contents to a file.

    Interface Event(Methods) class Methods
    ActionListener actionPerformed() ActionEvent ae getSource
    ItemListener itemStateChanged() ItemEvent ie getItem
    AdjustmentListener adjustmentValueChanged() AdjustmentEvent ae getAdjustable()
    MouseListener mouseEntered MouseEvent me getX()
    MouseAdapter mouseExited getY()
    mouseClicked getPoint()
    mousePressed getClickCount()
    mouseReleased getButton()
    MouseMotionListener mouseMoved MouseEvent me same as prev.
    MouseMotionAdapter mouseDragged
    KeyListener keyTyped KeyEvent ke getKeyChar()
    KeyAdapter keyPressed getKeyCode()
    FocusListener focusGained FocusEvent fe getSource()
    FocusAdapter focusLost
    WindowListener windowOpend WindowEvent we
    WindowAdapter windowClosing

    Java uses event delegation model for event handling. In the event delegation model a component may be told which object or should be notified when the component generates a particular kind of event.

    If a component is not interested in an event type, then events of that type will not be propagated. The delegation model is based on the following key concepts:

    • Event classes.
    • Event listeners.
    • Adapters.

    Event classes:

    Clicking on a button results in an event. The event has lot of information associated with it, like: time, component generating the event, co-ordinates of the point at which clicking took place etc.

    This information is put inside an event class. The concept is similar to exception handling. Whenever an exception occurs an object of appropriate exception class is created and all the exception related information is put into this object.

    We have hierarchy of event classes, which is somewhat similar to the hierarchy of exception classes. Whenever some event occurs, it is detected by event handling, mechanism, which creates objects of appropriate event class and puts all the event related information into it.

    Event listeners:

    Event listeners are responsible for taking appropriate action when an event occurs. An event listener is an object to which a component has delegated the task of handling a particular kind of event.

    When the component experiences input, an event of the appropriate type is constructed, the event is then passed as the parameter to a method call on the listener. A listener must implement the interface that contains that event handling method.

    We can compare the event listeners with the catch blocks for handling the exceptions. We need to provide a catch block corresponding to the exception to be handled. Similarly if we want to handle an event generated by some component, we need to provide appropriate listener.

    The listeners are associated with the components. Different events have different listeners. When event occurs, an appropriate listener method is called and event object is passed as parameter.


    The listeners defined in the language are interfaces only. We need to provide implementation of the appropriate listener. Some listeners have 2 or more methods.

    To implement a listener having more than one method, we need to provide implementation of all the methods even if we are interested only in one method. The adapter classes provide the solution to this problem.

    An adapter class provides dummy implementation of all the methods of a listener. This helps in reducing programming efforts. If we want to use just one method of a listener class containing say 5 methods then we can simply extent the adapter class and override the desired method.

    Event hierarchy:

    The AWT has a well-defined event hierarchy. Normally the classes corresponding to leaf nodes in the event hierarchy represent the actual events. While non-leaf classes act as base classes and encapsulate.

    AWT event hierarchy is described in the following diagram.


    The java.util.eventObject class:

    This is the top most super class of all the event classes. It is a very general class, with only one method of interest, which returns the object that originated the event.

    Object getsource()

    The java.awt.AWTEvent Class:

    One sub class of event object is java.awt.AWTEvent, which is the super class of all the delegation model event classes. Again, there is only one method of interest, which return the id of the event.

    int getID()

    An event’s ID is an int that specifies the exact nature of the event. For example, an instance of the mouseevent class can represent one of seven occurrences: click , Drag, Entrance, Move, Exit, Press, Release. Each of these possibilities is represented by an int MouseEvent . MOUSE_CLICKED, MouseEvent.MOUSE_DRAGGED, and so on.

    The sub classes of java.awt.AWTEvent represent the various event types that can be generated by the various AWT components, and contain all necessary information relating to the activity that triggered the event.

    The non-super class event types (i.e. those that are actually fired by component) are:

    1. ActionEvent:

      general by activation of components for example .it is generated on button click, on double-click on list item and on pressing enter in a TextField.

    2. AdjustmentEvent:

      generated by adjustment of adjustable components such as scroll bars.

    3. ContainerEvent:

      generated when components are added to or removed from a container.

    4. FocusEvent:

      generated when a component receives or loses input focus.

    5. ItemEvent:

      generated when an item is selected from a list, choice, or checkbox.

    6. KeyEvent :

      generated by keyboard activity.

    7. MouseEvent:

      generated by mouse activity.

    8. MouseWheelEvent

      An event, which indicates that the mouse wheel was rotated in a component.

    9. WindowEvent:

      generated by window activity (such as iconifying or deiconifying.)

    10. TextEvent:

      generated when a text component is modified.

    The java.awt.event package:

    This package provides the interfaces and classes for dealing with different types of events fired by AWT components. The events are fired by event sources. An event listener registers with an event source to receive notifications about the events of a particular type.

    This package defines events and event listeners, as well as event listener adapters, which are convenience classes to make easier the process of writing event listeners.

    Event Listener Interfaces:

    Each event class has a corresponding listener interface and defines one or more methods. The most commonly used listener interfaces are described in the following sections.

    The Action Listener Interface:

    This is the listener interface for receiving action events. The class that is interested in processing an action event implement this interface, and the object created with that class is registered with a component, using the component’s addActionListener() method. When the action event occurs, the action listener’s action performed () method is invoked whose signatures are:

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.