Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
Page 4 of 6
Event handling occurs in the processEvent() method, which is invoked by the JSF implementation during the Handle Request Events phase. That method checks to see if the event's name is clicked (the event generated by the decode() method). If so, the processEvent() method calls the component's toggleImage() method, which toggles the image displayed by the component. When the component subsequently renders, it will display the
new selected image.
The preceding example illustrates how you can implement custom components with JavaServer Faces; however, the component in Listing 12 handles rendering and event handling on its own, so it's not as flexible as it could be. The component, for example, cannot fit with a different renderer, and therefore cannot generate markup other than HTML. Although it's a little more work, separating rendering and event handling from components greatly increases those components' flexibility, as discussed in the next section.
This section describes how to separate rendering and event handling from the custom component discussed in the previous section. Separating components from their rendering and event handling increases reuse and flexibility and lets you attach a different renderer to a component to generate a representation of that component in another markup language.
In Listing 13, I've rewritten the component in Listing 12.
package com.sabreware.components;
import javax.faces.component.UIGraphic;
import com.sabreware.eventHandlers.ToggleGraphicEventHandler;
public class UIToggleGraphic extends UIGraphic {
// This component supports one command: click
private static String clickCommand = "click";
public UIToggleGraphic() {
addRequestEventHandler(new ToggleGraphicEventHandler());
}
public boolean getRendersSelf() {
return false;
}
public String getComponentType() {
return "com.sabreware.components.UIToggleGraphic";
}
public String getRendererType() {
return "ToggleGraphicHTMLRenderer";
}
public String getClickCommandName() {
return clickCommand;
}
// This private method toggles the component's image.
public void toggleImage() {
String imageOne = (String)getAttribute("imageOne");
String imageTwo = (String)getAttribute("imageTwo");
String currentImage = (String)getAttribute("image");
if(imageTwo.equals(currentImage))
setAttribute("image", imageOne);
else
setAttribute("image", imageTwo);
// The setURL method is defined in the superclass (UIGraphic)
setURL((String)getAttribute("image"));
}
}
The preceding component delegates rendering and event handling to other objects. It indicates that delegation by overriding
the getRendersSelf() method to return false.
The component's constructor creates an event handler and adds that event handler to the component's event handlers list. The
component also overrides the getRendererType() method to return an identifier for the component's renderer.
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq