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

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Create client-side user interfaces in HTML, Part 2

The HTMLPane sources

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.

Extend JEditorPane with the Factory Method pattern

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.

1 | 2 | 3 | 4 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. pc2phone/web2phone/callcentre/callshop/callback/ca
By apnavoip
0 05/14/08 08:24 AM
by voipswitch
. VIPPIE Dialer For VOIPSWITCH only by Solution4VOIP
By solution4voip_net
0 05/01/08 07:41 AM
by solution4voip_net
. Latest versions of voipswitch with original files
By apnavoip
0 10/10/07 07:02 AM
by apnavoip
. ok here goes
By amy_b
0 06/13/07 05:13 PM
. my own article?
By JavaPracticum
2 03/28/07 06:40 AM
by Rocco
. The sourceforge.net can't be visited,why?
By kendy
0 10/16/06 02:32 AM
by kendy
. Small problem...
By Green_Monkey23
0 10/03/06 08:46 PM
by Anonymous
. Please stop the Jerry Springer antics
By Troy
( Pages 1 2 all )
22 03/02/06 05:35 PM
by Anonymous
. PROVOCATIVE BUT INTERESTING AUTHOR
By Anonymous
1 03/02/06 05:12 PM
by Anonymous
. Pointless
By Anonymous
5 03/02/06 12:10 PM
by Anonymous
. question
By cassie1
1 03/02/06 07:43 AM
by cassie1
. How to configure Pentaho.war file in jboss4.0.3
By kannan
1 03/01/06 10:01 PM
by kannan
. change dafult button in any page
By rohit_java
0 07/13/04 12:51 AM
by rohit_java
. "path =..."
By ekjon
1 07/01/04 07:03 AM
by rohit_java
. JEditorPane submits forms?
By Blue Adept
0 11/15/03 09:31 PM
by Anonymous


Resources