O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required