Solved

Setting a Text Color on JTextPane

Posted on 2003-11-13
5
2,429 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
[X]
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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 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