
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert Styled Text in JTextPane Component
In this article we will learn to insert styled text into a JTextPane component in Java. The JTextPane component is a versatile text editor that supports rich text features, including the ability to style text with different fonts, colors, and sizes. By leveraging the StyledDocument and Style classes, you can insert styled text dynamically into a JTextPane using Java Swing.
Insert styled text in a JTextPane component using the SimpleAttributeSet and StyleConstants classes. With that, we will also use StyledDocument.
Java Swing
The Swing API is a collection of flexible GUI components designed to simplify the development of Java-based front-end applications. Built on top of the AWT API, it serves as a more advanced alternative, offering counterparts for nearly all AWT controls while enhancing functionality.
Key Components of Styled Text in JTextPane
-
JTextPane: A text component capable of handling styled text.
-
StyledDocument: Manages the structure and content of the styled text in JTextPane.
-
Style: Represents a set of attributes (e.g., font, color, size) that can be applied to text.
- StyleConstants: A helper class for setting style attributes such as:
- setForeground(Color): Sets the text color.
- setBold(boolean): Makes text bold.
- setItalic(boolean): Makes text italic.
To insert styled text in a JTextPane Component
Getting the StyledDocument of the JTextPane ?
StyledDocument doc = textPane.getStyledDocument();Defining and applying styles ?
Style style = textPane.addStyle("BoldRed", null;Inserting the styled text ?
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), "This is bold and red text.\n", style);
Example
The following is an example of inserting styled text in a JTextPane component ?
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class SwingDemo { public static void main(String args[]) throws BadLocationException { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); JTextPane textPane = new JTextPane(); textPane.setForeground(Color.blue); textPane.setBackground(Color.cyan); SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setItalic(set, true); textPane.setCharacterAttributes(set, true); textPane.setText("This is our text pane! "); Font font = new Font("Verdana", Font.BOLD, 18); textPane.setFont(font); StyledDocument doc = (StyledDocument) textPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setBold(style, true); doc.insertString(doc.getLength(), "Demo text", style); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane = new JScrollPane(textPane); container.add(scrollPane, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
Output
Conclusion
Using JTextPane and StyledDocument, you can easily insert styled text into your Java Swing applications. This feature is especially useful for creating chat interfaces, editors, or any application requiring rich text formatting.