Writing HTML

The write( ) method from JEditorPane takes advantage of the writer installed as part of the HTMLEditorKit. In our previous example, that writer is the HTMLWriter class. Starting with a more generic styled document, you could also write HTML with MinimalHTMLWriter. The classes described in this section both extend from the AbstractWriter class. (See Figure 23-9.)

HTML document-writing class diagram

Figure 23-9. HTML document-writing class diagram

The AbstractWriter Class

In this chapter, we’ve talked about a variety of strategies for saving document content. As of SDK 1.2, a new class provides some assistance in creating a rendition of an in-memory document structure suitable for saving as human-readable text. It relies on the ElementIterator class. AbstractWriter supports indentation to clarify the document structure as well as maximum line length to keep the generated output easy to read.

ElementIterator is a simple iterator class (somewhat obviously) devoted to working with Element objects. It has the usual next( ) and previous( ) methods of any bidirectional iterator. Both return objects of type Element. Unlike the new iterators in the Collections API, there is no hasNext( ) method. Instead, next( ) or previous( ) return null to signal the “end” of the stream. As with the constructors for AbstractWriter, an ElementIterator can be built on a Document or start from a particular Element. This class is covered ...

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.