Java JTextPane Last Updated : 28 Apr, 2025 Comments Improve Suggest changes 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 JTextPane chinmaya121221 Follow Improve Article Tags : Java Geeks Premier League java-swing Geeks Premier League 2023 Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Like