1

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.

user513951
  • 12,445
  • 7
  • 65
  • 82
  • Clone: http://stackoverflow.com/questions/564759/how-to-redirect-all-console-output-to-a-gui-textbox – Jérôme Feb 19 '09 at 11:52
  • If the given answers did not satisfy you in your previous post, you should have edit it instead of repost. – Jérôme Feb 19 '09 at 11:53
  • I'm new to this site and I commented on the answers given and got no response. I'll keep that in mind next time though. –  Feb 19 '09 at 11:55
  • See also: [How to redirect all console output to a GUI textbox?](http://stackoverflow.com/questions/564759) – Fabian Steeg Feb 24 '09 at 09:52

1 Answers1

6

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.

user24081
  • 99
  • 1
  • 3