Solved

Setting a Text Color on JTextPane

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

911 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

19 Experts available now in Live!

Get 1:1 Help Now