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
EditorKitclassDefining the
DocumenttypeDefining new
ActionsCreating custom
ViewclassesCreating a
ViewFactoryCreating a “reader” and a “writer”
Telling
JEditorPaneabout 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 ...