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
In "Create Client-Side User Interfaces in HTML, Part 1," I discussed in depth HTMLPane, a class that simplifies layout of client-side user interfaces by letting you specify them in HTML and providing a way to
submit a form to your program rather than a Web server. Now I conclude this series by examining HTMLPane code. You should read last month's article if you haven't already.
In this article, I also describe the Factory Method design pattern, which Java's JEditorPane class uses heavily.
Read the whole "Create Client-Side User Interfaces in HTML" series:
Note: You can download this article's associated source code from my Website.
The HTMLPane is an extension of javax.swing.JEditorPane that adapts it to do more than just display HTML text in a text control. Before I leap into the HTMLPane code, however, I need to explain how to customize a JEditorPane.
First, a disclaimer: I'm not a fan of the javax.swing.* packages' architecture. Swing is way too complex for what it does, and it's a good example of going crazy with design patterns
without considering whether the resulting system is usable or not. I would agree if you argued that the system I'm about to
describe could have been better designed. Other design patterns (such as Strategy) would have been better choices than the
patterns I used.
JEditorPane makes heavy use of the Factory Method pattern. A factory method creates an object that implements a known interface, but
you don't know the actual class of the object being created. A derived-class override of the base-class factory method can
supply a specialization of the default object.
The problem with the factory-method approach to customization can be seen in the JEditorPane. It's excruciating to change a JEditorPane's behavior in even trivial ways. Listing 1 shows what you must go through to add support for a custom tag to the JEditorPane class. I added support for a <today> tag, which displays today's date on the output screen. It's not a pretty picture.
An EditorKit, used internally by the JEditorPane, parses HTML. To recognize a custom tag, you must provide your own EditorKit. You do this by passing the JEditorPane object a setEditorKit(myCustomKit) message; the most convenient way to do that is to extend JEditorKit and set things up in the constructor (Listing 1, line 17). By default the JEditorKit uses an EditorKit extension called HTMLEditorKit, which does almost all of the work we need to do.
The main thing you must change is a ViewFactory, which the JEditorKit uses to build the visible representation of the HTML page. I created an HTMLEditorKit derivative called HTMLPane.SimplifiedHTMLPaneEditorKit that returns my custom view factory to the JEditorPane (Listing 1, line 23).
The SimplifiedHTMLPane.CustomViewFactory (Listing 1, line 31), overrides a single method, create(). Every time the JEditorPane recognizes a new HTML element in the input, it calls create, passing it an Element object that represents the element actually found. The create() method extracts the tag name from the Element. If the tag is a <today> tag (recognized on line 41), create() returns an instance of yet another class, a View, whose createComponent() method returns the Component displayed on the screen in place of the <today> tag.
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