Java Reference
In-Depth Information
or initiates some other action, the operating system detects this and passes the
information to the JVM, which in turn will create an event object that holds
information about the action. Then the JVM sends the event (or fires it) to your
program. You create event handling methods to receive the events and carry out
the particular operations of interest.
For the Java AWT, events are sent just to those objects that registered to receive
particular kinds of events. Such event receiver objects are called listeners .A
listener implements the proper interface for a given type of event and registers
itself with the components that generate that type of event. A class does not need
to be a subclass of Component to implement a listener interface. In the following
sections we will discuss the handling of events generated by components such
as buttons and menu items. (See the Sun tutorial [1] for further event listener
examples.)
7.2.1 Event listeners
As you move your mouse and click on its buttons, the operating system (OS)
detects the physical signal generated by each increment in the position of the
mouse and the press and release of the mouse buttons (a “click” is actually two
events - button down and button up). The OS sends a message with data (e.g. the
position of the cursor) about each such event to the component object in the AWT
system of the JVM, which then checks for event listeners registered to receive
those types of events occurring over a particular component.
There are different kinds of listeners for different kinds of events. This is
organized by providing an interface specific to each type of event. Examples
include:
ActionListener - GUI button clicks
MouseListener - mouse button clicks
MouseMotionListener - mouse movements
WindowListener - closing a frame
These interfaces are sub-interfaces of the EventListener interface. Most
event listener types are in the java.awt.event package, though some newer
event listener types appear in the javax.swing.event package.
The ActionListener interface has just one method: actionPer-
formed(ActionEvent ae) .For a GUI button you will need to implement
the ActionListener and provide an actionPerformed() method to carry
out the operations desired when that GUI button is clicked.
Classes that generate events maintain a list of event listeners. Whenever an
event occurs, it will be sent to each event listener in that list. The order in which
events are delivered is completely up to the JVM. All classes that maintain event
listener lists will provide methods to add new event listeners and remove existing
Search WWH ::




Custom Search