The code that enables my to throw the system console outputs into the Jtextarea:
Code:import java.awt.*; import java.io.*; import javax.swing.*; public class SystemConsole extends JFrame { PipedInputStream piOut; PipedInputStream piErr; PipedOutputStream poOut; PipedOutputStream poErr; JTextArea systemConsole; public SystemConsole(JTextArea systemOutput ) throws IOException { systemConsole=systemOutput; // Set up System.out piOut = new PipedInputStream(); poOut = new PipedOutputStream(piOut); System.setOut(new PrintStream(poOut, true)); // Set up System.err piErr = new PipedInputStream(); poErr = new PipedOutputStream(piErr); System.setErr(new PrintStream(poErr, true)); // Create reader threads new ReaderThread(piOut).start(); new ReaderThread(piErr).start(); } class ReaderThread extends Thread { PipedInputStream pi; ReaderThread(PipedInputStream pi) { this.pi = pi; } public void run() { final byte[] buf = new byte[1024]; try { while (true) { final int len = pi.read(buf); if (len == -1) { break; } SwingUtilities.invokeLater(new Runnable() { public void run() { systemConsole.append(new String(buf, 0, len)); // Make sure the last line is always visible systemConsole.setCaretPosition(systemConsole.getDocument().getLength()); // Keep the text area down to a certain character size int idealSize = 1000; int maxExcess = 500; int excess = systemConsole.getDocument().getLength() - idealSize; if (excess >= maxExcess) { systemConsole.replaceRange("", 0, excess); } } }); } } catch (IOException e) { } } } }




Reply With Quote