Solved

Changing text color in JTextArea

Posted on 1998-06-02
8
596 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
Industry Leaders: 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!

 
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

Industry Leaders: 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 Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

734 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