JTextPane is a class in Java Swing which provides a flexible and extensible way to display and edit styled text. It is a subclass of JEditorPane, which in turn is a subclass of JTextComponent.
JTextPane allows you to display and edit documents that are styled with HTML, RTF (Rich Text Format), or your own custom markup language. You can apply different styles to different parts of the text, such as font, color, size, and style (bold, italic, underline, etc.).
To use JTextPane, you first need to create an instance of it, then set the text content and styles using various methods provided by the class. For example, you can use the setText
method to set the text content, and the setCharacterAttributes
method to apply styles to specific parts of the text.
Here is an example of creating a JTextPane and setting its text and styles:
JTextPane textPane = new JTextPane(); textPane.setText("Hello, world!"); StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false); SimpleAttributeSet red = new SimpleAttributeSet(); StyleConstants.setForeground(red, Color.RED); doc.setCharacterAttributes(0, 5, red, false);
In this example, we first create a JTextPane and set its text to “Hello, world!”. We then get the StyledDocument
object from the text pane using the getStyledDocument
method. We create two SimpleAttributeSet
objects to define the styles we want to apply. The center
attribute set centers the text, and the red
attribute set makes the first five characters of the text red.
We then use the setParagraphAttributes
method to apply the center
attribute set to the entire text, and the setCharacterAttributes
method to apply the red
attribute set to the first five characters.
This is just a simple example of what you can do with JTextPane. There are many other methods and classes you can use to customize the appearance and behavior of the text pane.
Constructors of Java JTextPane:
JTextPane is a subclass of JEditorPane and inherits its constructors. In addition to the constructors inherited from JEditorPane, JTextPane provides two additional constructors:
JTextPane()
: Creates a new JTextPane with a default styled document.JTextPane(StyledDocument doc)
: Creates a new JTextPane with the specified styled document.
Here’s an example of how to use these constructors:
// Create a JTextPane with a default styled document JTextPane textPane1 = new JTextPane(); // Create a new styled document StyledDocument doc = new DefaultStyledDocument(); // Add some content to the document try { doc.insertString(0, "Hello, world!", null); } catch (BadLocationException e) { e.printStackTrace(); } // Create a JTextPane with the specified styled document JTextPane textPane2 = new JTextPane(doc);
In this example, we first create a JTextPane using the default constructor. This creates a new JTextPane with a default styled document. We then create a new StyledDocument
object using the DefaultStyledDocument
class and add some content to it using the insertString
method.
Finally, we create a second JTextPane using the StyledDocument
object we just created. This creates a new JTextPane with the specified styled document.
Note that you can also set the styled document of a JTextPane using the setStyledDocument
method after the JTextPane has been created.
Useful Methods of Java JTextPane:
JTextPane provides a number of useful methods for displaying and editing styled text. Here are some of the most commonly used methods:
setText(String text)
: Sets the text content of the JTextPane.getStyledDocument()
: Returns the styled document associated with the JTextPane.setStyledDocument(StyledDocument doc)
: Sets the styled document of the JTextPane.setCharacterAttributes(AttributeSet attrs, boolean replace)
: Applies the specified attributes to the currently selected text or to the insertion point.setParagraphAttributes(AttributeSet attrs, boolean replace)
: Applies the specified attributes to the current paragraph or to the paragraphs containing the selected text.insertComponent(Component comp)
: Inserts the specified component into the text at the current insertion point.insertIcon(Icon icon)
: Inserts the specified icon into the text at the current insertion point.replaceSelection(String content)
: Replaces the currently selected text with the specified content.getCaretPosition()
: Returns the current position of the caret in the text.setCaretPosition(int pos)
: Sets the position of the caret in the text.setEditable(boolean editable)
: Sets whether the JTextPane is editable or not.setContentType(String type)
: Sets the type of content that the JTextPane can display and edit. The supported types are “text/plain”, “text/html”, and “text/rtf”.
Here’s an example of how to use some of these methods:
// Create a JTextPane JTextPane textPane = new JTextPane(); // Set the text content textPane.setText("Hello, world!"); // Set the font and color of the first five characters StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet attrs = new SimpleAttributeSet(); StyleConstants.setFontFamily(attrs, "Arial"); StyleConstants.setForeground(attrs, Color.RED); doc.setCharacterAttributes(0, 5, attrs, false); // Insert an icon at the current caret position Icon icon = new ImageIcon("image.png"); textPane.insertIcon(icon); // Replace the currently selected text with "Goodbye, world!" textPane.replaceSelection("Goodbye, world!"); // Get the current caret position int pos = textPane.getCaretPosition(); // Set the editable property to false textPane.setEditable(false);
In this example, we first create a JTextPane and set its text content using the setText
method. We then get the StyledDocument
associated with the JTextPane and use the setCharacterAttributes
method to set the font and color of the first five characters.
We then use the insertIcon
method to insert an icon at the current caret position, and the replaceSelection
method to replace the currently selected text with “Goodbye, world!”.
Finally, we get the current caret position using the getCaretPosition
method and set the editable
property of the JTextPane to false using the setEditable
method.
JTextPane Example:
Here’s a simple example of using JTextPane to display and edit styled text:
import javax.swing.*; import javax.swing.text.*; public class JTextPaneExample { public static void main(String[] args) { // Create a JFrame and JTextPane JFrame frame = new JFrame("JTextPane Example"); JTextPane textPane = new JTextPane(); // Create a styled document StyledDocument doc = textPane.getStyledDocument(); // Add some styled text to the document Style style = doc.addStyle("default", null); StyleConstants.setFontFamily(style, "Arial"); StyleConstants.setFontSize(style, 16); StyleConstants.setForeground(style, Color.BLUE); try { doc.insertString(doc.getLength(), "Hello, ", style); style = doc.addStyle("italic", style); StyleConstants.setItalic(style, true); doc.insertString(doc.getLength(), "world!", style); style = doc.addStyle("underline", style); StyleConstants.setUnderline(style, true); doc.insertString(doc.getLength(), " This text is underlined.", style); } catch (BadLocationException e) { e.printStackTrace(); } // Set the size of the text pane and add it to the frame textPane.setPreferredSize(new Dimension(400, 200)); frame.add(textPane); // Set some frame properties and show the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
In this example, we first create a JFrame
and a JTextPane
. We then create a StyledDocument
associated with the text pane and add some styled text to the document using the insertString
method and the various StyleConstants
methods to set the font family, font size, and color of the text, as well as whether it is italicized or underlined.
We then set the size of the text pane and add it to the frame. Finally, we set some frame properties and show the frame. When we run the program.