- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set style for JTextPane in Java?
To set style for text in JTextPane, use setItalic() or setBold() that sets italic or bold style for font respectively.
Following is our JTextPane component −
JTextPane pane = new JTextPane();
Now, use the StyleConstants class to set style for the JTextPane we created above. We have also set the background and foregound color −
SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); StyleConstants.setForeground(attributeSet, Color.black); StyleConstants.setBackground(attributeSet, Color.orange); pane.setCharacterAttributes(attributeSet, true);
The following is an example to set style for JTextPane −
Example
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; 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.StyleConstants; 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 pane = new JTextPane(); SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); StyleConstants.setForeground(attributeSet, Color.black); StyleConstants.setBackground(attributeSet, Color.orange); pane.setCharacterAttributes(attributeSet, true); pane.setText("We are learning Java and this is a demo text!"); JScrollPane scrollPane = new JScrollPane(pane); container.add(scrollPane, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to set font face, style, size and color for JTextPane text in Java
- How to set font for text in JTextPane with Java?
- How to set default background color for JTextPane in Java?
- How to use JTextPane to style code in Java?
- How to set foreground color for different words in a JTextPane
- Java Program to format text in JTextPane
- Set style for Labelframe in Python Tkinter
- Java Program to set the Font and Color of some text in a JTextPane using Styles
- Set style for pagination with CSS
- How to set the style for line drawing using imagesetstyle() function in PHP?
- How to set JAVA_HOME for Java in Windows?
- How to set JAVA_HOME for Java in Linux?
- How to set FlowLayout for JFrame in Java?
- Set image for bullet style with CSS
- Java Program to insert styled text in a JTextPane Component

Advertisements