Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Setting a Text Color on JTextPane

Posted on 2003-11-13
5
Medium Priority
?
2,440 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 80 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 80 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

610 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