The JTextPane Class

JTextPane is a multiline text component that can display text with multiple fonts, colors, and even embedded images. It supports named hierarchical text styles and has other features that can help implement a word processor or a similar application.

Technically, JTextPane is a subclass of JEditorPane, but it usually makes more sense to think of JTextPane as a text component in its own right. JEditorPane uses “editor kits” to handle text in various formats (such as HTML or RTF) in a modular way. Use a JEditorPane when you want to view or edit text in one of these formats. Use a JTextPane when you want to handle the text yourself. We’ll cover JEditorPane and editor kits (including JTextPane’s editor kit, StyledEditorKit) in Chapter 23.

Properties

JTextPane defines the properties shown in Table 22-1. document and styledDocument are both names for the same property. The model returned by getDocument( ) always implements StyledDocument (which is an interface that extends Document). Attempting to call setDocument( ) with a Document that is not a StyledDocument throws an IllegalArgumentException. (The Document and StyledDocument interfaces are described in detail later in this chapter.)

Table 22-1. JTextPane properties

Property

Data type

get

is

set

Default value

characterAttributes

AttributeSet

·

   

document o

Document

·

 

·

Value of styledDocument

editorKitb, o

EditorKit

·

 

·

StyledEditorKit

inputAttributes

MutableAttributeSet

·

   

logicalStyle

Style

·

 

·

 

paragraphAttributes

AttributeSet ...

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.