Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Start customizing Swing's editor pane -- patch the Swing HTMLEditorKit

Learn how to add hyperlink enter/exit events to the Swing HTMLEditorKit

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
If you've played around with the Swing component set at all, you have undoubtedly seen the JEditorPane class displaying some pretty spiffy HTML documents. JEditorPane doesn't render HTML all by itself, though. It uses a group of classes that make up what Swing refers to as an editor kit.

The editor kit attached to a JEditorPane tells the pane how to draw text, images, and components based on a stream of input data -- like an HTML file. It also dictates how the pane will react to user input. For example, when displaying an HTML document, JEditorPane uses the HTMLEditorKit class.

The HTMLEditorKit class delegates parsing and rendering responsibilities to a group of HTML-aware classes. The event handling is done directly in HTMLEditorKit itself. The editor kit has a mouse listener, and when mouse clicks occur, HyperlinkEvent objects are generated. This means that not only do images show up, but the hyperlinks actually work! Well, almost.

The HyperlinkEvent class appears to support three subcategories of hyperlink events -- entered, exited, and activated -- so, you should be able to write a pretty quick listener class that does all the work for you: changing the mouse pointer when it passes over a link and jumping to a new document when the user clicks on the link. SimpleLinkListener is just such a class. (Of course, this version also has a few extras built in to allow you to track current and future sites, just as you've come to expect from a real browser.)

With the listener attached to an editor pane, you'll have a minimal browser functioning in less than 30 lines of code. The TestBrowser class shows a slightly more interesting browser (see below) written with only 75 lines of code. Not bad, eh?

Your new mini-browser showing off hyperlink enter/exit events

If you want to take this app out for a quick test drive, just click on the button below. Of course, you'll need to have Swing installed on your system. See the sidebar, Installing Swing, for step-by-step instructions on the installation process.

One thing to watch out for, though. The HTML 3.2 support in the HTMLEditorKit is not quite perfect yet. While this area is under constant improvement, you may find some HTML pages which throw errors during formatting. (They throw a javax.swing.text.StateInvariantError to be precise.) It shouldn't stop the page from loading, but you might not see everything exactly where the authors wanted it go. Most basic HTML pages do not display these errors.

You need a Java-enabled browser to see this applet.

Now, this little app could easily serve as a help system or in-lined browser, but there's a small problem, which you've likely already noticed: although jumping to links works great, the mouse cursor never changes as you move it over links. In addition, the status bar is never updated with the preview URL. In fact, the enter and exit link events aren't generated at all. I'm not really sure why this happens; I suppose it could be an oversight in the design. At any rate, it's frustrating and it needs to be fixed.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Download the complete source code for this article as a zip file http://www.javaworld.com/jw-01-1999/swing/jw-01-swing.zip
  • The code in this article relies on the Swing 1.1 beta 3 release, which you can download from the Java Developer Connection (free registration required) http://java.sun.com/jdc
  • Read Sun's Swing applet page to find out how to run Swing applets on Netscape and Internet Explorer http://java.sun.com/products/jfc/swingdoc-current/applets.html