Tengo una clase (que se muestra a continuación) que se extiende JPanel
y contiene un JTextPane
. Quiero redirigir System.out
y System.err
a mi JTextPane
. Mi clase no parece funcionar. Cuando lo ejecuto, redirige las impresiones del sistema, pero no se imprimen en mi JTextPane
. ¡Por favor ayuda!Redireccionamiento System.out a JTextPane
Nota: Las llamadas solo se redirigen cuando se inicia la aplicación. Pero en cualquier momento después del lanzamiento, las llamadas System.out
no se redirigen al JTextPane
. (es decir, si coloco un System.out.prinln();
en la clase, se ejecutará, pero si se coloca en un actionListener
para su uso posterior, no se redirige).
public class OSXConsole extends JPanel {
public static final long serialVersionUID = 21362469L;
private JTextPane textPane;
private PipedOutputStream pipeOut;
private PipedInputStream pipeIn;
public OSXConsole() {
super(new BorderLayout());
textPane = new JTextPane();
this.add(textPane, BorderLayout.CENTER);
redirectSystemStreams();
textPane.setBackground(Color.GRAY);
textPane.setBorder(new EmptyBorder(5, 5, 5, 5));
}
private void updateTextPane(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Document doc = textPane.getDocument();
try {
doc.insertString(doc.getLength(), text, null);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
textPane.setCaretPosition(doc.getLength() - 1);
}
});
}
private void redirectSystemStreams() {
OutputStream out = new OutputStream() {
@Override
public void write(final int b) throws IOException {
updateTextPane(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextPane(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
}
He borrado mi respuesta porque era incorrecta. – jjnguy
¿Ve algunas llamadas redirigidas y otras no? – jjnguy
solo las llamadas desde el interior de la clase OSXConsole se imprimen en el JTextPane. – Jakir00