Java Reference
In-Depth Information
So how do you define a listener? You can make the objects of any class listener objects by making the
class implement a listener interface . There are quite a variety of listener interfaces, to cater for different
kinds of events. In the case of our button click, the ActionListener interface needs to be
implemented to receive the event from the button. The code that is to receive this event object and
respond to the event is implemented in a method declared in the listener interface. In our example, the
actionPerformed() method in the ActionListener interface is called when the event occurs, and
the event object is passed as an argument. Each kind of listener interface defines particular methods for
receiving the events that that listener has been designed to deal with.
Simply implementing a listener interface isn't sufficient to link the listener object to an event source.
You still have to connect the listener to the source, or sources, of the events that you want it to deal
with. You register a listener object with a source by calling a particular method in the source object. In
this case, we call the addActionListener() method for the JButton object, and pass the listener
object as an argument to the method.
This mechanism for handling events using listeners is very flexible, and very efficient, particularly for GUI
events. Any number of listeners can receive a particular event. However, a particular event is only passed to
the listeners that have registered to receive it, so only interested parties are involved in responding to each
event. Since being a listener just requires a suitable interface to be implemented, you can receive and handle
events virtually anywhere you like. The way in which events are handled in Java, using listener objects, is
referred to as the delegation event model . This is because the responsibility for responding to events that
originate with a component, such as a button or a menu item, is not handled by the objects that originated the
events themselves - but is delegated to separate listener objects.
Not all event handling necessarily requires a separate listener. A component can handle its own
events, as we shall see a little later in this chapter.
A very important point to keep in mind when writing code to handle events is that all such code
executes in the same thread, the event-dispatching thread. This implies that while your event-handling
code is executing, no other events can be processed. The code to handle the next event will only start
executing when the current event-handler finishes. Thus the responsiveness of your program to the user
is dependent on how long your event-handling code takes to execute. For snappy performance, your
event handlers must take as little time as possible to execute.
Let's now get down to looking at the specifics of what kinds of events we can expect, and the range of
listener interfaces that process them.
Event Classes
There are many different kinds of events to which your program may need to respond - from menus,
buttons, from the mouse, from the keyboard, and a number of others. In order to have a structured
approach to handling events, these are broken down into subsets. At the topmost level, there are two
broad categories of events in Java:
Low-level Events - these are events that arise from the keyboard or from the mouse, or events
associated with operations on a window such as reducing it to an icon or closing it. The
meaning of a low-level event is something like 'the mouse was moved', 'this window has been
closed' or 'this key was pressed.
Search WWH ::




Custom Search