Colors on JTextArea

Hi, how can I change the color of the text in a JTextArea. I need to have different colors for different lines. Is for a Log on which errors must be red and normal settings on green.
[189 byte] By [MelGohana] at [2007-11-26 18:42:52]
# 1
You can't do that with a TextArea.Use a TextPane instead. http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html
zadoka at 2007-7-9 6:16:49 > top of Java-index,Java Essentials,Java Programming...
# 2
ok I will check it. Thanks a lot.
MelGohana at 2007-7-9 6:16:49 > top of Java-index,Java Essentials,Java Programming...
# 3

Ok I made a bit with this but is still not working.

I definen a JTExtPane, I also definen some styles (colors), now I can change the color of the selected text, but when I add new text everything turns red again. How can I make the color to stay.

This is my code:

private StyleContext creaEstilos() {

StyleContext sc = new StyleContext();

estiloRojo = sc.addStyle( null,null );

StyleConstants.setForeground( estiloRojo,Color.red );

estiloVerde = sc.addStyle( null,null );

StyleConstants.setForeground( estiloVerde,Color.green );

estiloAzul = sc.addStyle( null,null );

StyleConstants.setForeground( estiloAzul,Color.blue );

return( sc );

}

on the main

TP=new JTextPane ();

TP.setBounds (20, 20, 500, 500);

this.getContentPane().add (TP);

TP.setText("Hola como estan");

estilo = null;

creaEstilos();

estilo = estiloRojo;

TP.setCharacterAttributes(estilo,false );

TP.setText (TP.getText()+"\nEstamos bien");

If I write the text, well it works, but if I use a setText (getText ()+"new TExt");

everything goes into one single color.

What can I do?

MelGohana at 2007-7-9 6:16:49 > top of Java-index,Java Essentials,Java Programming...
# 4

Sorry this is my code: I am sleeping.

package server.log;

import java.awt.Color;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextPane;

import javax.swing.text.Style;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyleContext;

public class LogPane extends JFrame implements MouseListener{

/**

* @param args

*/

Style estiloRojo;

Style estiloAzul;

Style estiloVerde;

Style estilo;

JTextPane TP;

public LogPane () {

this.setLayout(null);

JButton boton=new JButton ("cambio");

TP=new JTextPane ();

TP.setBounds (20, 20, 500, 500);

this.getContentPane().add (TP);

TP.setText("Hola como estan");

estilo = null;

creaEstilos();

estilo = estiloRojo;

TP.setCharacterAttributes(estilo,false );

TP.setText (TP.getText()+"\nEstamos bien");

this.addMouseListener(this);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new LogPane ().show ();

}

private StyleContext creaEstilos() {

StyleContext sc = new StyleContext();

estiloRojo = sc.addStyle( null,null );

StyleConstants.setForeground( estiloRojo,Color.red );

estiloVerde = sc.addStyle( null,null );

StyleConstants.setForeground( estiloVerde,Color.green );

estiloAzul = sc.addStyle( null,null );

StyleConstants.setForeground( estiloAzul,Color.blue );

return( sc );

}

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

String text=TP.getText();

int ini=text.length();

TP.setText(text+"Nuevo texto de color verde");

TP.select(ini, TP.getText().length());

estilo=estiloVerde;

TP.setCharacterAttributes(estilo,true);

TP.select(ini, ini);

}

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

}

Message was edited by:

MelGohan

MelGohana at 2007-7-9 6:16:49 > top of Java-index,Java Essentials,Java Programming...
# 5
Swing related questions should be posted in the Swing forum.> If I write the text, well it works, but if I use a setText (getText ()+"new TExt");use the Document.insertString(...) method to append text to the end of the document.
camickra at 2007-7-9 6:16:49 > top of Java-index,Java Essentials,Java Programming...
# 6

mmmm well, sorry if I posted in the wrong place, but for not doing a cross post I will continue here.

I did this:

TP is my JTextPane

TP.getDocument ().insertString (.......);

The problem is the AttributeSet that I must pass as parameter, The API says that its an interface that represents a set of configurations and styles, but how can I use it?

Please help me with this I am lost.

MelGohana at 2007-7-9 6:16:49 > top of Java-index,Java Essentials,Java Programming...
# 7

> The API says that its an interface that represents a set of configurations and styles, but how can I use it?

Did you search the Swing forum for examples? Use the method name as the search keyword to find examples.

SimpleAttributeSet keyWord = new SimpleAttributeSet();

StyleConstants.setForeground(keyWord, Color.RED);

StyleConstants.setBackground(keyWord, Color.YELLOW);

StyleConstants.setBold(keyWord, true);

...

try

{

doc.insertString(doc.getLength(), "\nEnd of text", keyWord );

}

catch(Exception e) {}

camickra at 2007-7-9 6:16:49 > top of Java-index,Java Essentials,Java Programming...