Smita,
позаботьтесь о том, чтобы вставить фрагмент своего кода, чтобы можно было понять, где именно проблема, или требуется помощь.
Приходя к вашей проблеме,
Насколько мне известно, нет способа установить разные цвета для разных текстовых элементов в textArea в Java. Вы можете установить только один цвет для всех.
Альтернативой является использование JTextPane.
Посмотрите, поможет ли вам следующий код.
String text = "Some Text..."; //This can be any piece of string in your code like
output of your program...
JTextPane myTextPane = new JTextPane();
SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
// As what error you were referring was not clear, I assume there is some code in your
program which pops out some error statement. For convenience I use Exception here..
if( text.contains("Exception") ) //Checking if your output contains Exception...
{
StyleConstants.setForeground(sas, Color.red); //Changing the color of
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());
}
}
Полагаю, это решит вашу проблему с помощью нескольких модификаций.
Спасибо.
Sushil