Una soluzione potrebbe essere la seguente: all'interno del metodo init() dell'applet setti l'output generale su di un oggetto di tipo "AreaOutputStream" (vedi codice più avanti):
codice:
AreaOutputStream area;
JTextArea tuaJTextArea;
...
public void init() {
tuaJTextArea = new JTextArea();
...
area = new AreaOutputStream( tuaJTextArea );
System.setOut( area );
System.setErr( area );
}
E questo è il codice dell'AreaOutputStream
codice:
import java.io.*;
import javax.swing.JTextArea;
public class AreaOutputStream extends OutputStream {
private JTextArea jta = null;
private PrintStream log = null;
private boolean attivo;
private boolean useLog;
public AreaOutputStream(JTextArea jta) {
this(jta, null);
}
public AreaOutputStream(JTextArea jta, String nomeFileLog) {
this.jta = jta;
attivo = true;
useLog = (nomeFileLog != null);
if ( useLog ) {
try {
log = new PrintStream( new FileOutputStream(nomeFileLog) );
} catch (Exception e) {
useLog = false;
e.printStackTrace();
}
}
}
public void close() throws IOException {
attivo = false;
if ( useLog ) {
log.close();
}
}
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
public void write(byte[] b, int off, int len) throws IOException {
if ( attivo ) {
String s = "";
for(int i=0; i<len; i++) s += (char) b[i+off];
if (jta != null) {
jta.append(s);
if ( useLog ) {
log.print( s );
log.flush();
}
} else {
throw new IOException("No JTextArea specified for output");
}
}
}
public void write(int b) throws IOException {
if ( attivo ) {
if (jta != null) {
jta.append("" + ((char) b));
if ( useLog ) {
log.print("" + ((char) b));
log.flush();
}
} else {
throw new IOException("No JTextArea specified for output");
}
}
}
}
Qualunque System.out verrà redirezionato nella JTextArea.
Se usi AWT e non Swing, ovviamente, il codice andrà riadattato.
Ciao.