Solved

Changing text color in JTextArea

Posted on 1998-06-02
8
600 Views
Last Modified: 2008-02-01
Is it possible to change a color of a single line in Swing's JTextArea?

I tried something like

                Color color = processOutputTextArea.getCaretColor();

                processOutputTextArea.setCaretColor(Color.red);

                processOutputTextArea.append("\n" + input + "\n");

                processOutputTextArea.setCaretColor(color);

 but that does not have any effect.

And how about font?

Any ideas?
0
Comment
Question by:msmolyak
[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
  • 5
  • 3
8 Comments
 
LVL 5

Author Comment

by:msmolyak
ID: 1221885
Edited text of question
0
 
LVL 4

Accepted Solution

by:
evijay earned 70 total points
ID: 1221886


  MutableAttributeSet attr = new SimpleAttributeSet();
  StyleConstants.setForeground(attr, fg);
  setCharacterAttributes(editor, attr, false); // where editor is JEditorPane

For more info, see the stylepad example that comes with the swing 1.0.x release.


0
 
LVL 5

Author Comment

by:msmolyak
ID: 1221887
Are you implying that changing font and color on character by character basis is not possible with JTextArea?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 5

Author Comment

by:msmolyak
ID: 1221888
How do I write a string to a JEditorPane (or JTextPane), i.e. what is a method equivalent to JTextArea.append()?
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221889
You need to use JEditorPane instead of JTextArea.

 * A TextArea is a multi-line area that displays plain text.
 * It is intended to be a lightweight component that provides source
 * compatibility with the java.awt.TextArea class where it can
 * reasonably do so.  This component has capabilities not found in
 * the java.awt.TextArea class.  The superclass should be consulted for
 * additional capabilities.  Alternative multi-line text classes with
 * more capabilitites are JTextPane and JEditorPane.

So, it is not possible to change the color of single line in JTextArea.
Use JEditorPane instead

0
 
LVL 5

Author Comment

by:msmolyak
ID: 1221890
Do you mind presenting a usable example?  All I am trying to do is to write to that text component and be able to change font and color (that is the component is not editable). I could not figure out how to write to the JEditorPane (or JTextPane which is used in Stylepad).
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221891
reating a JTextPane for complex text display requires two simple steps:

                                      1.Create a DefaultStyledDocument to model for the data

                                            DefaultStyledDocument doc = new DefaultStyledDocument();

                                      2.Create a JTextPane using the DefaultStyledDocument

                                            JTextPane pane = new JTextPane (doc);

                                  Once you have a your document, you can make various AttributeSet
                                  objects to describe the content style:

                                  SimpleAttributeSet defaultStyle =
                                    new SimpleAttributeSet();

                                  SimpleAttributeSet italicStyle =
                                    new SimpleAttributeSet();
                                  StyleConstants.setItalic(attr, true);

                                  SimpleAttributeSet bigStyle =
                                    new SimpleAttributeSet();
                                  StyleConstants.setFontSize(attr, 36);

                                  and fill up the JTextPane, associating an attribute set with each paragraph
                                  in the StyledDocument:

                                  doc.insertString (doc.getLength(),
                                    "Hello World\n", bigStyle);
                                  doc.insertString (doc.getLength(),
                                    "What's up Doc?\n", italicStyle);
                                  doc.insertString (doc.getLength(),
                                    "Boring...\n", defaultStyle);

For more details, please go to this url

http://developer.javasoft.com/developer/onlineTraining/swing2/swing.html#JFCSwingText

0
 
LVL 5

Author Comment

by:msmolyak
ID: 1221892
Works beautifully! Thanks.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

631 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