tags:

views:

665

answers:

2

I currently have a program that prints lines of text to the screen in various manners such as 'System.out.println()' statements and for loops the print all elements in an array to screen.

I am now adding a GUI to this program in a seperate class. My problem is that I want to print everything that prints to eclipse's console to a textbox in my GUI instead. Is this possible and if so how would I go about doing this.

Thanks.

+1  A: 

If you really want to do this, set the System OutputStream to a PipedOutputStream and connect that to a PipedInputStream that you read from to add text to your component, for example:

PipedOutputStream pOut = new PipedOutputStream();
System.setOut(new PrintStream(pOut));
PipedInputStream pIn = new PipedInputStream(pOut);
BufferedReader reader = new BufferedReader(new InputStreamReader(pIn));

You can then read from the reader and write it to your text component, for example:

while(appRunning) {
    try {
        String line = reader.readLine();
        if(line != null) {
            // Write line to component
        }
    } catch (IOException ex) {
        // Handle ex
    }
}

I'd suggest that you don't use System.out for your application output though, it can be used by anything (e.g. any third party libraries you decide to use). I'd use logging of some sort (java.util.logging, Log4J etc) with an appropriate appender to write to your component.