Java - sacar caracteres en color con JTextPane

 
Vista:

sacar caracteres en color con JTextPane

Publicado por rita (7 intervenciones) el 16/02/2010 23:08:50
Buenas
Tengo una lista con caracteres por ejemplo {vvvv1112121rrvrrvvvvee22} y kiero sacar esta lista por un JtextPane. kiero sacar por ejemplo las v's en rojo y los 1's en azul y las r's en verde, cada caracter en un color diferente. para ello he hecho :

// esto no me funciona pk no me muestra nada en el JtextPane

final String res=null;
StyleContext sc = new StyleContext();
final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
final JTextPane area = new JTextPane(doc);


final Style heading2Style = sc.addStyle("Heading2", null);
heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

for (int i=0; i<resultado.size();i++){

res =resultado.get(i).toString();
if ( res == String.valueOf('v')){
doc.insertString(0, res, null);
doc.setParagraphAttributes(0, 1, heading2Style, false);
area.setForeground(Color.blue);
area.setFont(new Font("serif", Font.PLAIN, 12));
}
// aqui no sé que hacer para verificar si son otros caracteres pues poner otro //color y mostrarlos
}

espero que alguin me pueda ayudar
gracias amigos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder