Setting a Text Color on JTextPane

Posted on 2003-11-13
Last Modified: 2011-09-20
I dont know exactly how to put a color on a string into the JTextPane?

Example :

JTextPane p1 = new JTextPane();

String x1 = " Hello ";
String x2 = " World ";

// How do I set the color of the 1st & 2nd String ?
// I want x1 to be in color red & x2 to be blue ?


Is it possible how ?
Question by:Rosewell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

yongsing earned 20 total points
ID: 9745427
Set the content type to HTML, then use the FONT tag to set the color:

JTextPane p1 = new JTextPane();
p1.append("<font color='blue'>" + x1 + "</font>");
p1.append("<font color='red'>" + x2 + "</font>");

Author Comment

ID: 9745440
What if I use JTextArea is it possible ?
LVL 92

Expert Comment

ID: 9745483
> What if I use JTextArea is it possible ?

LVL 92

Assisted Solution

objects earned 20 total points
ID: 9745493
> I dont know exactly how to put a color on a string into the JTextPane?

try {
        // Get the text pane's document
        JTextPane textPane = new JTextPane();
        StyledDocument doc = (StyledDocument)textPane.getDocument();
        // Create a style object and then set the style attributes
        Style style = doc.addStyle("StyleName", null);
        // Font size
        StyleConstants.setFontSize(style, 30);
        // Background color
        // Foreground color
        StyleConstants.setForeground(style, Color.white);
        // Append to document
        doc.insertString(doc.getLength(), "Some Text", style);
    } catch (BadLocationException e) {

LVL 92

Expert Comment

ID: 9745526

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question