A Custom EditorKit

Although Swing’s HTML support is less than ideal, it is sufficient to handle inline help systems and aid in rapid prototyping. Each release of the SDK improves support and usability. It will continue to get better. In the meantime, if you’re desperate for serious markup language support, you really should check out XML.

If you’re interested in doing your own EditorKit work, look up the more detailed HTMLEditorKit chapters online. You should also check out the javax.swing.text.rtf package. It serves the same basic purpose as the HTML package but reads and writes RTF files. However, be aware that RTF seems to be even more plagued with “acceptable variants” than HTML. Make sure you test your output on an intended target system before rolling out your new commercial editor!

To round out this final section, we’ll review the steps involved in creating your own editor kit. These steps include:

  • Creating the EditorKit class

  • Defining the Document type

  • Defining new Actions

  • Creating custom View classes

  • Creating a ViewFactory

  • Creating a “reader” and a “writer”

  • Telling JEditorPane about your new kit

Create the EditorKit Class

First, create your EditorKit class. Depending on the type of documents you’re going to support, you can subclass the abstract EditorKit base class or extend any of the existing kits we’ve covered in this chapter.

Much of the work in creating this class is covered in the steps that follow. In addition to those more complex issues, you should implement the following ...

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.