Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textarea color

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
I am building a chat. Is it possible to have different rows in different colors in a textarea? Is there any solution, like adding colored labels into a textarea?

I am java newbie!

Thanks / Henrik
 
hi

to set the foreground color of the text, use the setForeground() method of the textarea object.
example, t.setForeground(Color.red) or t.setForeground(Color.green) etc.

do this everytime u want to change the color, and add the text u want.

luv
Karthik.
 
If you want to get a bit more advance, look into implementing an JEditorPane or JTextPane. Then you can assign the editorkits and styles, work with the document breaking the different areas into various root and other elements. It actually gets quite complicated. Or...(this idea just came to me..) try adding new JLabel objects to the area, each with a different color.

Tom
 
Thanks for your answers. But ;

taTest.setForeground(Color.red);
taTest.append("test");
taTest.setForeground(Color.blue);
taTest.append("ing");
Will result in giving the whole sentence "testing" the color blue.

/ Henrik
 
Right... a text area can only have one foreground color and one background color at any given time. Hence the suggestion to use a JEditorPane or JTextPane. If you want several different text colors in a single component, you're definitely going to need a more sophisticated component than a JTextArea.

Best,
Adam Rice
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top