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?
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.
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.
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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.