Solved

Changing text color in JTextArea

Posted on 1998-06-02
8
583 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
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

708 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

15 Experts available now in Live!

Get 1:1 Help Now