Smita,
take care to insert the code snippet so that you can understand exactly where the problem is or need help.
Getting to your problem
As far as I know, there is no way to set different colors for different text elements in textArea in java. You can set only one color for all.
An alternative is to use JTextPane.
See if the following code helps your case.
String text = "Some Text...";
output of your program...
JTextPane myTextPane = new JTextPane();
SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
program which pops out some error statement. For convenience I use Exception here..
if( text.contains("Exception") )
{
StyleConstants.setForeground(sas, Color.red);
StyleConstants.setItalic(sas, true);
try
{
myTextPane.getDocument().insertString
(
myTextPane.getDocument().getLength(),
text + "\n",
sas
);
}
catch( BadLocationException ble )
{
text.append(ble.getMessage());
}
}
else
{
StyleConstants.setForeground(sas, Color.GREEN);
try
{
myTextPane.getDocument().insertString
(
myTextPane.getDocument().getLength(),
text + "\n",
sas
);
}
catch(BadLocationException ble)
{
text.append(ble.getMessage());
}
}
I think this will solve your problem with a few changes.
Thank.
Sushil
source
share