Download
FAQ History |
API
Search Feedback |
Handling Events for Custom Components
As explained in Implementing an Event Listener, events are automatically queued on standard components that fire events. A custom component, on the other hand, must manually queue events from its
decode
method if it fires events.Performing Decoding explains how to queue an event on
MapComponent
in itsdecode
method. This section explains how to write the class representing the event of clicking on the map and how to write the method that processes this event.As explained in Understanding the JSP Page, the
actionListener
attribute of themap
tag points to thechooseLocaleFromMap
method of the beanLocaleBean
. This method processes the event of clicking the image map. Here is thechooseLocaleFromMap
method ofLocaleBean
:public void chooseLocaleFromMap(ActionEvent actionEvent) { AreaSelectedEvent event = (AreaSelectedEvent) actionEvent; String current = event.getMapComponent().getCurrent(); FacesContext context = FacesContext.getCurrentInstance(); context.getViewRoot().setLocale((Locale) locales.get(current)); ... }When the JavaServer Faces implementation calls this method, it passes in an
ActionEvent
that represents the event generated by clicking on the image map. Next, it casts it to anbookstore6/src/listeners/AreaSelectedEvent
. Then this method gets theMapComponent
associated with the event. It then gets the value of theMapComponent
'scurrent
attribute, which indicates the currently selected area. The method then uses the value of thecurrent
property to get theLocale
object from aHashMap,
which is constructed elsewhere in theLocaleBean
class. Finally the method sets the locale of theFacesContext
to theLocale
obtained from theHashMap
.In addition to the method that processes the event, you need the event class itself. This class is very simple to write: You have it extend
ActionEvent
and provide a constructor that takes the component on which the event is queued and a method that returns the component. Here is theAreaSelectedEvent
class used with the image map:public class AreaSelectedEvent extends ActionEvent { ... public AreaSelectedEvent(MapComponent map) { super(map); } public MapComponent getMapComponent() { return ((MapComponent) getComponent()); } }As explained in the section Creating Custom Component Classes, in order for the
MapComponent
to fire events in the first place, it must implementActionSource
. BecauseMapComponent
extendsUICommand
, it also implementsActionSource
.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.