Solved

Changing text color in JTextArea

Posted on 1998-06-02
8
597 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

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