Events

In this section, you will learn how to handle events in Java awt. Events are the integral part of the java platform. You can see the concepts related to the event handling through the example and use methods through which you can implement the event dri

Events

In this section, you will learn how to handle events in
Java awt. Events are the integral part of the java platform. You can see the concepts related to the event handling through
the example and use methods through which you can implement the event driven application.

For any event to occur, the objects
registers themselves as listeners. No event takes place if there is no
listener i.e. nothing happens when an event takes place if there is no listener.
No matter how many listeners there are, each and every listener is capable of
processing an event. For example, a SimpleButtonEvent applet registers itself as the listener for the button's action
events that creates a Button instance.

ActionListener can be implemented by any Class
including Applet. One point to remember here is that all the listeners are always notified.
Moreover, you can also call AWTEvent.consume() method whenever you don't
want an event to be processed further. There is another method which is used by
a listener to check for the consumption. The method is isConsumed() method. The
processing of the events gets stopped with the consumption of the events by the
system once a listener is notified. Consumption only works for InputEvent and its
subclasses. Moreover, if you don't want any input from the user through keyboard
then you can use consume() method for the KeyEvent.

The step by step procedure of Event handling is as
follow:

When anything interesting happens then the subclasses of
AWTEvent are generated by the component.

Any class can act like a Listener class permitted by
the Event sources. For example, addActionListener() method is used
for any action to be performed, where Action is the event type. There
is another method by which you can remove the listener class which is
removeXXXListener() method, where XXX is the event type.

A listener type has to be implemented for an event
handling such as ActionListener.

There are some special type of listener types as
well for which you need to implement multiple methods like key Events.
There are three methods which are required to be implemented for Key events
and to register them i.e. one for key release, key typed and one for key
press. There are some special classes as well which are known as adapters
that are used to implement the listener interfaces and stub out all the methods.
these adapter classes can be sub classed and and can override the necessary
method.

AWTEvent

Most of the times every event-type has Listener
interface as Events subclass the AWTEvent class. However, PaintEvent
and InputEvent don't have the Listener interface because only the paint()
method can be overriden with PaintEvent etc.

Low-level Events

A low-level input or window operation is represented
by the
Low-level events. Types of
Low-level events are mouse movement, window opening, a key press etc. For
example, three events are generated by typing the letter 'A' on the Keyboard one for releasing, one for pressing, and one for
typing. The
different type of low-level events and operations that generate each event
are show below in the form of a table.

FocusEvent

Used for Getting/losing focus.

MouseEvent

Used for entering, exiting,
clicking, dragging, moving, pressing, or releasing.

The interaction with GUI component is represented by
the
Semantic events like changing the text of a text field, selecting a button
etc. The different events generated by different components is shown
below.

ItemEvent

Used for state changed.

ActionEvent

Used fordo the command.

TextEvent

Used fortext changed.

AdjustmentEvent

Used for value adjusted.

Event Sources

If a component is an event source for something then
the same happens with its subclasses. The different event sources are
represented by the following table.

Every listener interface has at least one event type.
Moreover, it also contains a method for each type of event the event class
incorporates. For example as discussed earlier, the KeyListener
has three methods, one for each type of event that the KeyEvent has:
keyTyped(), keyPressed(), and keyReleased().