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( ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.