Java JTextPane Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like 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 JTextPaneMethods 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 JTextPaneFollowing 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: 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: Comment More infoAdvertise with us Next Article Java JDesktopPane C chinmaya121221 Follow Improve Article Tags : Java Geeks Premier League java-swing Geeks Premier League 2023 Practice Tags : Java Similar Reads Java JEditorPane A JEditorPane is a component in Java Swing that provides a simple way to display and edit HTML and RTF (Rich Text Format) content. It is a versatile component that allows you to display styled text, links, and images, making it useful for creating simple web browsers, rich-text editors, and content 3 min read Java JTabbedPane JTabbedPane is a GUI(Graphical User Interface) component in the Java Swing library that allows you to create a tabbed pane interface. A tabbed pane is a container that can store and organize multiple components into distinct tabs. It contains a collection of tabs. When you click on a tab, only data 5 min read Java JRootPane In Java Swing, JRootPane is a fundamental component that serves as the root container for creating complex user interfaces. It encapsulates the core functionality of a top-level window, like JFrame, and provides a structure for organizing the content within a GUI application. In Java JRootPane is a 3 min read Java JDesktopPane Java JDesktopPane is a part of the Swing library that helps Java Developers to make desktop-like applications, It allows the developers to manage and organize child frames within a parent frame. In this article, we are going to see some constructors, methods, fields, and some examples of JDesktopPan 4 min read Java JOptionPane In Java, JOptionPane is a part of the Java Swing library. It helps us to create dialog boxes such as message dialogs, conformation dialogs, input dialogs, and options dialogs In this article, we are going to explore some constructors, methods, and some examples of JOptionPane. Constructors of JOptio 5 min read Java JLayeredPane In Java Swing, JLayeredPane is a powerful container that allows you to overlay and manage multiple components within a single container. Each component in a JLayeredPane can occupy a different layer, and you can control their positions and visibility. In this article, we are going to implement the J 4 min read Like