Start of Tutorial > Start of Trail > Start of Lesson | Search |
Two Swing classes support styled text:JEditorPane
and its subclassJTextPane
. Several facts about editor panes and text panes were sprinkled throughout the previous four sections. Here we list the facts again, to collect them in one place and to provide a bit more detail. The information here should help you understand the differences between editor panes and text panes, and when to use which.The next section provides API tables for all text components, including editor panes and text panes, and a list of examples that use text components.
- An editor pane or a text pane can easily be loaded with text from a URL using the
setPage
method. TheJEditorPane
class also provides constructors that let you initialize an editor pane from a URL.JTextPane
has no such constructors. See Using an Editor Pane to Display Text from a URL for an example of using this feature to load an uneditable editor pane with HTML.Be aware that the document and editor kit might change when using the
setPage
method. For example, if an editor pane contains plain text (the default), and you load it with HTML, the document will change to anHTMLDocument
instance and the editor kit will change to anHTMLEditorKit
instance. If your program uses thesetPage
method, make sure the code adjusts for possible changes to the pane's document and editor kit instances (re-register document listeners on the new document, and so on).
- Editor panes, by default, know how to read, write, and edit plain, HTML, and RTF text. Text panes inherit this capability but impose certain limitations. A text pane insists that its document implement the
StyledDocument
interface.HTMLDocument
andRTFDocument
are bothStyledDocuments
so HTML and RTF work as expected within a text pane. If you load a text pane with plain text though, the text pane's document is not aPlainDocument
as you might expect, but aDefaultStyledDocument
.
- To support a custom text format, implement an editor kit that can read, write, and edit text of that format. Then call the
registerEditorKitForContentType
to register your kit with theJEditorPane
class. By registering an editor kit in this way, all editor panes and text panes in your program will be able to read, write, and edit the new format. However, if the new editor kit is not aStyledEditorKit
, text panes will not support the new format.
- As mentioned previously, a text pane requires that its document be an instance of a class that implements the
StyledDocument
interface. The Swing text package provides a default implementation of this interface,DefaultStyledDocument
, which is the document text panes use by default. A text pane also requires that its editor kit be an instance of aStyledEditorKit
(or a subclass). Be aware that theread
andwrite
methods forStyleEditorKit
write plain text.
- Through its styled document and styled editor kit, text panes provide support for named styles and logical styles. The
JTextPane
class itself contains many methods for working with styles that simply call methods in its document or editor kit.
- Through the API provided in the
JTextPane
class, you can embed images and components in a text pane. You can embed images in an editor pane, too, but only by including the images in an HTML or RTF file.
Start of Tutorial > Start of Trail > Start of Lesson | Search |