O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extending HTMLEditorKit

As a quick example of how we might extend this class to add some functionality of our own, let’s look at an editor kit that spits out debugging information as we load documents. This allows us to see the steps involved in extending an editor kit, and it leaves us with a very useful tool for implementing other extensions (such as custom tags and attributes).

The first step, of course, is to create our extended editor kit. In this example, we create a debugging editor kit that spits out the styles loaded and the individual tags it passes by. Here’s the code:

 // DebugHTMLEditorKit.java // A simple extension of the HTMLEditor kit that uses a verbose ViewFactory. import javax.swing.*; import javax.swing.text.*; import javax.swing.text.html.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.*; import java.io.Serializable; import java.net.*; public class DebugHTMLEditorKit extends HTMLEditorKit { public static HTML.Tag ORA = new HTML.UnknownTag("ora"); public static AttributeSet currentAnchor; public void install(JEditorPane paneEditor) { super.install(paneEditor); StyleSheet ss = getStyleSheet( ); java.util.Enumeration e = ss.getStyleNames( ); while (e.hasMoreElements( )) { System.out.println(e.nextElement( )); } } public ViewFactory getViewFactory( ) { return new VerboseViewFactory( ); } public static class VerboseViewFactory extends HTMLEditorKit.HTMLFactory { public View create(Element elem) { System.out.print("Element: " + elem.getName( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required