How to set output stream to TextArea

Dan picture Dan · Oct 18, 2012 · Viewed 34.1k times · Source

I'm trying to create a GUI Panel for a program and I'd like everything, that would normally print to my command prompt, to print to a TextArea object. I have the GUI panel formatted for the most part, I can't get the text to print to the TextArea though, here's my file:

package guipanel;

import javax.swing.*;
import java.awt.*;
import java.io.*;


/**
 *
 * @author Dan
 */
public class GUIPanel extends JFrame { 
    public GUIPanel() {
        initComponents();
    }
    private void setOutputStream(boolean catchErrors) {
        System.setOut(aPrintStream); 
        setVisible(true);
        requestFocus();
        if (catchErrors) {
               System.setErr(aPrintStream);
        }
    }
    private void addTabs(JTabbedPane jTabbedPane1) {
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        JPanel jPanel3 = new JPanel();
        JPanel jPanel4 = new JPanel();
        jTabbedPane1.add("Main", textArea1);
        jTabbedPane1.add("Commands", jPanel);
        jTabbedPane1.add("Rules", jPanel1);
        jTabbedPane1.add("Links", jPanel2);
        jTabbedPane1.add("Information", jPanel3);
        jTabbedPane1.add("Shutdown", jPanel4);
        setOutputStream(true);
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {

        textArea1 = new java.awt.TextArea();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        textArea1.setPreferredSize(new java.awt.Dimension(432, 343));
        textArea1.getAccessibleContext().setAccessibleParent(jTabbedPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Evolution-X 639");
        setBounds(new java.awt.Rectangle(0, 0, 400, 450));
        setResizable(false);
        getContentPane().setLayout(new java.awt.FlowLayout());

        addTabs(jTabbedPane1);
        getContentPane().add(jTabbedPane1);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPanel().setVisible(true);
            }
        });
    }
    private JMenu jMenu1;
    private JMenu jMenu2;
    private JMenuBar jMenuBar1;
    private JTabbedPane jTabbedPane1;
    private TextArea textArea1;
    private JPanel jPanel = new JPanel();
    private PrintStream aPrintStream  =
       new PrintStream(
         new FilterOutputStream(
           new ByteArrayOutputStream()));
}

Answer

MadProgrammer picture MadProgrammer · Oct 18, 2012

You need to redirect the print stream to an output stream you can control...

This is an example of concept I developed for an application I'm working on for work. We use this to bring up the output console when it's running at user sites so we can see what's being sent to standard out...until we fixed our logging that is ;)

Basically it puts a custom OutputStream in between the print stream and the console to capture the output, but still allows the content to printed to the console. This is helpful if you're running the program from the command line or IDE. You could put a switch to stop this if you wanted...

enter image description here

public class TestRedirect {

    public static void main(String[] args) {
        new TestRedirect();
    }

    public TestRedirect() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                CapturePane capturePane = new CapturePane();
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(capturePane);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                PrintStream ps = System.out;
                System.setOut(new PrintStream(new StreamCapturer("STDOUT", capturePane, ps)));

                System.out.println("Hello, this is a test");
                System.out.println("Wave if you can see me");
            }            
        });
    }

    public class CapturePane extends JPanel implements Consumer {

        private JTextArea output;

        public CapturePane() {
            setLayout(new BorderLayout());
            output = new JTextArea();
            add(new JScrollPane(output));
        }

        @Override
        public void appendText(final String text) {
            if (EventQueue.isDispatchThread()) {
                output.append(text);
                output.setCaretPosition(output.getText().length());
            } else {

                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        appendText(text);
                    }
                });

            }
        }        
    }

    public interface Consumer {        
        public void appendText(String text);        
    }

    public class StreamCapturer extends OutputStream {

        private StringBuilder buffer;
        private String prefix;
        private Consumer consumer;
        private PrintStream old;

        public StreamCapturer(String prefix, Consumer consumer, PrintStream old) {
            this.prefix = prefix;
            buffer = new StringBuilder(128);
            buffer.append("[").append(prefix).append("] ");
            this.old = old;
            this.consumer = consumer;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            String value = Character.toString(c);
            buffer.append(value);
            if (value.equals("\n")) {
                consumer.appendText(buffer.toString());
                buffer.delete(0, buffer.length());
                buffer.append("[").append(prefix).append("] ");
            }
            old.print(c);
        }        
    }    
}

enter image description here enter image description here

Updated with working example. Test on Windows 7, Java 6 and Mac OS Lion Java 7