Solved

Changing text color in JTextArea

Posted on 1998-06-02
8
592 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 106
Groovy problem when using SOAPUI : DispatchException occurred 7 60
servlet filter example 37 72
tomcat startup error 5 106
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

830 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