Solved

Setting a Text Color on JTextPane

Posted on 2003-11-13
5
2,405 Views
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 ?

p1.append(x1);
p1.append(x2);

Is it possible how ?
0
Comment
Question by:Rosewell
  • 3
5 Comments
 
LVL 9

Accepted Solution

by:
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.setContentType("text/html");
p1.append("<font color='blue'>" + x1 + "</font>");
p1.append("<font color='red'>" + x2 + "</font>");
0
 

Author Comment

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

Expert Comment

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

nope
0
 
LVL 92

Assisted Solution

by:objects
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
        StyleConstants.setBackground(style, Color.blue);
   
        // Foreground color
        StyleConstants.setForeground(style, Color.white);
   
        // Append to document
        doc.insertString(doc.getLength(), "Some Text", style);
    } catch (BadLocationException e) {
    }


0
 
LVL 92

Expert Comment

by:objects
ID: 9745526
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 60
changeXy challenge 13 57
array220 challenge 8 47
Java Timer (static) 9 15
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now