I l@ve RuBoard |
![]() ![]() |
22.1 The JTextPane ClassJTextPane 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. 22.1.1 PropertiesJTextPane 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.)
The inputAttributes, characterAttributes , paragraphAttributes, and logicalStyle properties are not properties of the JTextPane itself, but of the text under the caret. The types of these properties are related: the Style interface extends the MutableAttributeSet interface, which extends the AttributeSet interface. (We'll tackle these interfaces in Section 22.2 later in this chapter.) The values of these properties are stored by the pane's styledDocument[1] and typically change as the position of the caret moves. Because of this (and also because the setCharacterAttributes( ) and setParagraphAttributes( ) methods require an extra boolean parameter, which makes them invalid as JavaBeans property mutators), we elaborate on these methods later in this chapter in Section 22.1.3.
The editorKit property is inherited from JEditorPane, but there is little or no reason to deal with it directly. The editorKit defaults to an instance of StyledEditorKit. Attempting to set an editor kit that is not a StyledEditorKit (or a subclass) throws an IllegalArgumentException. 22.1.2 Constructors
22.1.3 Attribute and Style MethodsThese methods manipulate the character attributes, paragraph attributes, and logical styles of the pane's document. Character attributes include font and text color. Paragraph attributes can also include paragraph indentation and spacing. Styles are groups of attributes that can be reused throughout the document. Named styles are styles that have been assigned a name. The document keeps track of these styles so you can look them up and reuse or modify them later. Changing the attributes of a style changes all text that is tagged with that style. You can apply logical styles to whole paragraphs. Logical styles define a baseline set of attributes that can be overridden by local paragraph or character attributes. (See the discussion in Section 22.3.5 later in this chapter for more details.) When setting the logical style for a paragraph, you can use a named style to facilitate reuse. We do this in the example editor later in the chapter, presenting a menu of the named styles. Rather than operating on one attribute at a time, the attribute and style methods operate on collections of attributes held in objects implementing AttributeSet, MutableAttributeSet, or Style. The accessor methods may return null if there is no content at the caret position or if there are no attributes to return. The mutator methods are thread-safe, so (unlike most Swing methods) they can be called safely by threads other than the event-dispatching thread.
22.1.4 Insertion/Replacement MethodsIn addition to displaying text, JTextPane can display Icons and arbitrary Components. This flexibility makes it possible to display documents with embedded images or interactive documents such as HTML forms. There are three insertion methods: one inserts text, one inserts an Icon, and one inserts a Component. Despite their dissimilar names, all three work the same way. The new insertion replaces the current selection or, if there is no selection, appears at the current caret position. (If you want to insert at a position other than the caret position, you can do so by manipulating the pane's document directly.) If you want to insert without replacing the current selection, you need to "deselect" before calling one of these methods. Here's one way to do this: pane.getCaret( ).setDot(pane.getCaretPosition( )); These methods are thread-safe, so (unlike most Swing methods) they can be called safely by threads other than the event-dispatching thread.
Here's a short example showing how these methods work. We create a JTextPane with three buttons that insert a text string, button, and icon. Figure 22-1 shows it after it's been played with a bit. // PaneInsertionMethods.java // import javax.swing.*; import java.awt.BorderLayout; import java.awt.event.*; // Show how icons, components, and text can be added to a JTextPane. public class PaneInsertionMethods { public static void main(String[] args) { final JTextPane pane = new JTextPane( ); // Button to insert some text JButton textButton = new JButton("Insert Text"); textButton.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent event) { pane.replaceSelection("text"); } }); // Button to insert an icon final ImageIcon icon = new ImageIcon("bluepaw.gif"); JButton iconButton = new JButton(icon); iconButton.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent event) { pane.insertIcon(icon); } }); // Button to insert a button JButton buttonButton = new JButton("Insert Button"); buttonButton.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent event) { pane.insertComponent(new JButton("Click Me")); } }); // Layout JPanel buttons = new JPanel( ); buttons.add(textButton); buttons.add(iconButton); buttons.add(buttonButton); JFrame frame = new JFrame( ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane( ).add(pane, BorderLayout.CENTER); frame.getContentPane( ).add(buttons, BorderLayout.SOUTH); frame.setSize(360,180); frame.setVisible(true); } } Figure 22-1. JTextPane containing JButtons and ImageIcons![]() 22.1.5 Named Style MethodsJTextPane provides three methods for dealing with named styles, but it doesn't manage the styles itself, delegating this to its document instead. See the similarly named methods in the StyledDocument interface, the DefaultStyledDocument class, and the StyleContext class (later in this chapter) for more details on named styles.
![]() |
I l@ve RuBoard |
![]() ![]() |