Skip to main content

changing string text color in Java

7 replies [Last post]
araghuna
Offline
Joined: 2007-02-09
Points: 0

couldn't find any information on this from doing a search on color in this forum. A google search showed me ways of how to change color of text in JavaScript. I'm stumped on how to actually change a string color to make it more readable within a textbox in Java Swing. Here's the relevant code I attempted (note: the variable called answer is a string variable that is not null):

JTextArea textArea = new JTextArea();
textArea.setOpaque(true);
textArea.setBackground(Color.Blue);

textArea.setForeground(Color.BLACK);
textArea.setText(answer);
textArea.setBorder(CommonUI.createEtchedBorder());
textArea.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));

The string contained in the variable answer does not display black, but still shows up as gray. I was considering wrapping the string in a font object and managing its color that way but I'm not sure if that would work because I tried this before to change its size and that didn't work.

thanks in advance for any suggestions!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
uwaifogeorge
Offline
Joined: 2008-06-18
Points: 0

Hi all. This may be a belated response to the problem of setting Foreground color for a JTextArea object, but it is better late than never. I had the same problems earlier, i discovered my mistake which was that I disabled the JTextArea object which kept it from displaying the actual textArea color. So if you ara making the same mistake, simply use the [u]setEditable(false)[/u] instead of [u]setEnabled(false)[/u] method.

ie
textArea = new JTextArea(22,70);
myScrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

textArea.setBackground(color);
textArea.setForeground(color2);

textArea.setEditable(false);

Hope this helps.
George Uwaifo

Kleopatra

Hi folks,

Surfacing for a day just for playing the fun-spoiling role - this is a
forum highly focused to SwingLabs (extended Swing :-) questions. Please
keep the discussion about unrelated (basic Swing :-) issues to the more
general forums, like f.i. the Swing&AWT forum on this server, the Swing
forum over at Sun's developers pages or the appropriate newsgroup.

Thanks
Jeanette

jdnc-interest@javadesktop.org wrote:
> couldn't find any information on this from doing a search on color in this forum. A google search showed me ways of how to change color of text in JavaScript. I'm stumped on how to actually change a string color to make it more readable within a textbox in Java Swing. Here's the relevant code I attempted (note: the variable called answer is a string variable that is not null):
>
> JTextArea textArea = new JTextArea();
> textArea.setOpaque(true);
> textArea.setBackground(Color.Blue);
>
> textArea.setForeground(Color.BLACK);
> textArea.setText(answer);
> textArea.setBorder(CommonUI.createEtchedBorder());
> textArea.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
>
> The string contained in the variable answer does not display black, but still shows up as gray. I was considering wrapping the string in a font object and managing its color that way but I'm not sure if that would work because I tried this before to change its size and that didn't work.
>
> thanks in advance for any suggestions!
> [Message sent by forum member 'araghuna' (araghuna)]
>
> http://forums.java.net/jive/thread.jspa?messageID=204231
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

jorgenrapp
Offline
Joined: 2004-08-02
Points: 0

Your example is showing up I expect when I run it,
however if you want to mix many colors and styles you can look at JTextPane,
HTML strings doesn't work in JTextField, or JTextArea,
but does in JLabel and JEditorPane ( after doing setContent("text/html")).

example:
[code]
JFrame f = new JFrame("test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
JTextArea tat = new JTextArea();
tat.setForeground(Color.blue);
tat.setText("JTextArea\nLooks blue to me?");
// ta.setEnabled(false);
p.add(tat);

JTextPane textPane = new JTextPane();
StyledDocument doc = (StyledDocument)textPane.getDocument();

// Add a couple of styles
Style styleU = doc.addStyle("UnderLine", null);
Style styleQ = doc.addStyle("Question", null);
Style styleAns = doc.addStyle("Answer", null);

StyleConstants.setUnderline(styleU, true);
// set the Foreground, can define other attrs too.
StyleConstants.setForeground(styleQ, Color.BLUE);
StyleConstants.setForeground(styleAns, Color.RED);

doc.insertString(doc.getLength(), "JTextPane\n", styleU);
doc.insertString(doc.getLength(), "1. What about it?\n", styleQ);
doc.insertString(doc.getLength(), "Yeah that works.", styleAns);
p.add(textPane);

JComponent ta = new JLabel("" +
"JLabel
" +
"" +
"1. What about it?
" +
"
" +
"" +
"html works for JLabels " +
"
"+
"");
p.add(ta);

ta = new JEditorPane("text/html","" +
"JEditorPane with text/html content
" +
"" +
"1. What about it?
" +
"
" +
"" +
"html works for JEditorPane" +
"
"+
"");
p.add(ta);

f.add(p);
f.setSize(800, 400);
f.setVisible(true);
[/code]

araghuna
Offline
Joined: 2007-02-09
Points: 0

Thanks for the suggestions to both of you. I'll see what I can do about changing the structure in which the content is displayed and see if that helps. About using the tags, I thought using was deprecated, at least when I last checked the api docs, in one of the pages concerning java.lang.text, which is why I didn't think of using that in the first place. Anyhow, I thought setting style attributes or using style sheets was much preferable to using the tags, at least based on my experience with web design.
Any more suggestions/clarification is much appreciated. Thanks again.

araghuna
Offline
Joined: 2007-02-09
Points: 0

I meant in my last response, I thought using the font tags was deprecated. I realize that sentence didn't quite read right when I wrote it.

agusmba
Offline
Joined: 2007-01-28
Points: 0

Hi,
don't use , and use some font tags.

See http://java.sun.com/docs/books/tutorial/uiswing/components/html.html

At the end of that page there's also a link to
http://java.sun.com/docs/books/tutorial/uiswing/components/text.html
which you may find useful too.

Message was edited by: agusmba

araghuna
Offline
Joined: 2007-02-09
Points: 0

And I did also try wrapping the string in the variable answer with html tags and such, as follows:
(suppose answer = "some string")
answer = "

" + answer + "

" but all that ended up doing was showing the actual tags along with the (still gray contents of answer, *sigh*) in the resulting document when the application was run