Redirect System.out.println output to Jtextarea

Asked

Viewed 1,084 times

5

I’m having trouble making this redirect, let’s explain. I own a class called Engine.java, this class is responsible for doing my processing and in it has all the System.out.prints process, the problem is that I have now created a window using Java Swing, and I would like to call it this method, and wanted all the text of the System.out.print was for a JTextArea created by me. It is possible?

  • 1

    Do you want to intercept the output of the println pro Jtextarea? As soon as you ask, are so many println so? Maybe the work you’ll get doing this "hacking" would be what you could do to adapt the pro swing code. In Jtextarea option, it cannot be a Joptionpane?

1 answer

6


Yes we can!

Yes, it is possible to change the object System.out that usually goes to the console using the method System.setOut. Just then pass an instance of PrintStream to direct the content to its TextArea.

One problem you may face is that the builder of PrintStream receives as parameter a OutputStream.

Soon, you will need to create a OutputStream to direct written text, overriding the method write(byte), ie, you will have to work with bytes and not with String or characters.

One way to do this is by using a ByteArrayOutputStream to collect the written bytes and then turn into String again.

Sample code

Note an example I made below:

PrintStream original = System.out;
StringBuilder sb = new StringBuilder();
System.setOut(new PrintStream(new OutputStream() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    @Override
    public void write(int b) throws IOException {
        baos.write(b);
    }
    @Override
    public void flush() throws IOException {
        sb.append(baos.toString());
        baos.reset();
    }
    @Override
    public void close() throws IOException {
        sb.append(baos.toString());
        baos.reset();
    }
}, true));

//tudo o que for escrito será capturado
System.out.print("Capture ");
System.out.println("Isto ");
System.out.format("Para %s!", "Mim");

//restaurando o original para escrever no console
System.setOut(original);
System.out.format("Resultado capturado:%n%s", sb);

Basically, I did an implementation of OutputStream as mentioned above, which stores the data in an array of bytes.

When does the flush or close, the code adds the data to a StringBuilder. In your case, just change this to concatenate to the TextArea.

Note that I passed a second parameter to the PrintStream with the value true. This makes with the method flush be called that every time something is written in PrintStream. If you do not put this, the data will not be displayed as long as the buffer is not full because PrintStream uses a BufferedOutputStream internally, and it may be that some information is not displayed in your TextArea when you expected.

A warning

It is not considered good practice to write on System.out.

If your program is more than an exercise or academic project, you must use a log library such as Logback or Log4j.

These libraries are much more flexible and allow you to configure which data to log, what format, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.