Olá pessoal,
tenho uma dúvida em relação ao uso de estilos com OutputStream customizado. Com o código a seguir:public class TextComponentStream extends OutputStream {
private JTextComponent textComponent;
private Style error;
public TextComponentStream(JTextComponent textComponent, Style error) {
this.textComponent = textComponent;
this.error = error;
}
@Override
public void write(final int b) throws IOException {
write(new byte[] {(byte) b}, 0, 1);
}
@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
final String str = new String(b, off, len);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// trava aqui!!!
textComponent.getDocument().insertString(textComponent.getDocument().getLength(),
str,
error);
}
catch (BadLocationException e) {
e.printStackTrace();
}
}
});
}
}
...
System.setOut(new PrintStream(new TextComponentStream(result, null)));
System.setErr(new PrintStream(new TextComponentStream(result, SwingUtils.getErrorStyle())));
...
public class SwingUtils {
public static Style getErrorStyle()
{
StyleContext sc = new StyleContext();
Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
Style errorStyle = sc.addStyle("error", defaultStyle);
StyleConstants.setForeground(errorStyle, Color.red);
return errorStyle;
}
...
Obrigado.
[]'s

