Java Reference
In-Depth Information
Whenever the user of a graphical program types characters or uses the mouse
anywhere inside one of the windows of the program, the Java window manager sends
a notification to the program that an event has occurred. The window manager
generates huge numbers of events. For example, whenever the mouse moves a tiny
interval over a window, a Ȓmouse moveȓ event is generated. Events are also
generated when the user presses a key, clicks a button, or selects a menu item.
Most programs don't want to be flooded by boring events. For example, when a
button is clicked with the mouse, the mouse moves over the button, then the mouse
button is pressed, and finally the button is released. Rather than receiving lots of
irrelevant mouse events, a program can indicate that it only cares about button clicks,
not about the underlying mouse events. However, if the mouse input is used for
drawing shapes on a virtual canvas, it is necessary to closely track mouse events.
An event listener belongs to a class that is provided by the application
programmer. Its methods describe the actions to be taken when an event occurs.
Every program must indicate which events it needs to receive. It does that by
installing event listener objects. An event listener object belongs to a class that you
define. The methods of your event listener classes contain the instructions that you
want to have executed when the events occur.
To install a listener, you need to know the event source. The event source is the user
interface component that generates a particular event. You add an event listener
object to the appropriate event sources. Whenever the event occurs, the event source
calls the appropriate methods of all attached event listeners.
Event sources report on events. When an event occurs, the event source notifies all
event listeners.
408
409
Use JButton components for buttons. Attach an ActionListener to each
button.
Search WWH ::




Custom Search