Skip to Main Content
Java Swing, 2nd Edition
book

Java Swing, 2nd Edition

by Dave Wood, Robert Eckstein, Marc Loy, James Elliott, Brian Cole
November 2002
Intermediate to advanced content levelIntermediate to advanced
1278 pages
38h 26m
English
O'Reilly Media, Inc.
Content preview from Java Swing, 2nd Edition

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( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Threads, 3rd Edition

Java Threads, 3rd Edition

Scott Oaks, Henry Wong

Publisher Resources

ISBN: 0596004087Errata PageSupplemental Content