Open In App

Java JTextPane

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, JTextPane is a versatile component that is part of the Swing library for building graphical user interfaces. It extends JEditorPane and provides an editable text component with rich text formatting capabilities. JTextPane allows you to display and edit styled text, making it suitable for implementing text editors, document viewers, and other applications that require advanced text formatting. In Java JTextPane is a part of javax.swing package.

The constructor of Java JTextPane

Constructor

Description

JTextPane()

This is the default constructor that creates an empty JTextPane.

JTextPane(StyledDocument doc)

This constructor allows you to specify a pre-existing StyledDocument to be used as the document model for the JTextPane.

JTextPane(String text)

Creates a JTextPane with the specified initial text. The text is not styled in this case.

Methods of JTextPane

Methods

Description

setText(String text)

Sets the text content of the JTextPane.

getText()

Retrieves the text content of the JTextPane.

setStyledDocument(StyledDocument doc)

Sets the StyledDocument to manage the text content and styles.

getStyledDocument()

Retrieves the current StyledDocument used by the JTextPane.

insertComponent(Component comp)

Inserts a Swing component at the current caret position in the text pane.

insertIcon(Icon icon)

Inserts an icon at the current caret position in the text pane.

setCharacterAttributes(AttributeSet attr, boolean replace)

Applies character attributes to the selected text or at the current caret position.

replaceSelection(String content)

Replaces the currently selected text with the specified content.

getSelectedText()

Retrieves the currently selected text in the text pane.

cut()

Cuts the selected text and places it in the clipboard.

copy()

Copies the selected text to the clipboard.

paste()

Pastes the content from the clipboard into the text pane.

replaceSelection(String content)

Replaces the currently selected text with the specified content.

getContentType()

Returns the content type of the text pane.

getCaret()

Retrieves the caret (cursor) position within the text pane.

setCaretPosition(int position)

Sets the caret position to the specified character index.

Examples of Java JTextPane

Following are the programs to implement Java JTextPane

Example 1:

Java




// Java Program to demonstrate a simple JTextPane
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
  
public class JTextPaneExample {
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> {
            // Create a JFrame
            JFrame frame = new JFrame("JTextPane Example");
            frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
  
            // Create a JTextPane
            JTextPane textPane = new JTextPane();
  
            // Step 2: Create styled text
            SimpleAttributeSet attributes
                = new SimpleAttributeSet();
            StyleConstants.setBold(attributes, true);
            StyleConstants.setItalic(attributes, true);
            textPane.setCharacterAttributes(attributes,
                                            false);
  
            // Step 3: Set the text
            textPane.setText(
                "This is a JTextPane example with styled text. !!Geeks Premier League 2023!!");
  
            // Step 4: Add JTextPane to the frame
            frame.add(new JScrollPane(textPane));
  
            frame.setVisible(true);
        });
    }
}


Output:

Java JTextPane

Example 2:

Java




// Java Program to Implement setStyledDocument,
// getStyledDocument, insertComponent methods of the
// JTextPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
  
public class JTextPaneExample {
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> {
            // Create a JFrame
            JFrame frame = new JFrame("JTextPane Example");
            frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
  
            // Create a JTextPane
            JTextPane textPane = new JTextPane();
  
            // Create a custom StyledDocument
            StyledDocument doc
                = textPane.getStyledDocument();
  
            // Create and set a Style for the text
            Style style = doc.addStyle("customStyle", null);
            StyleConstants.setForeground(style, Color.BLUE);
            StyleConstants.setBold(style, true);
  
            // Set the StyledDocument
            textPane.setStyledDocument(doc);
  
            // Insert text with custom style
            try {
                doc.insertString(0, "This is a ", style);
                doc.insertString(doc.getLength(),
                                 "button: ", null);
            }
            catch (BadLocationException e) {
                e.printStackTrace();
            }
  
            // Insert a button as a Swing component
            JButton button = new JButton("Click Me!");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    JOptionPane.showMessageDialog(
                        frame, "Button clicked!");
                }
            });
            textPane.insertComponent(button);
  
            // Add JTextPane to the frame
            frame.add(new JScrollPane(textPane));
  
            frame.setVisible(true);
        });
    }
}


Output:

Java JTextPane



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads