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?
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
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.
> 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) {}