StdErrOutWindows.java Source code

Java tutorial

Introduction

Here is the source code for StdErrOutWindows.java

Source

import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class StdErrOutWindows {
    JTextArea outArea = new JTextArea(20, 50), errArea = new JTextArea(20, 50);;

    public StdErrOutWindows() {
        JScrollPane pain = new JScrollPane(outArea);
        JFrame outFrame = new JFrame("out");
        outFrame.getContentPane().add(pain);
        outFrame.setVisible(true);

        pain = new JScrollPane(errArea);
        JFrame errFrame = new JFrame("err");
        errFrame.getContentPane().add(pain);
        errFrame.setLocation(errFrame.getLocation().x + 20, errFrame.getLocation().y + 20);
        errFrame.setVisible(true);

        System.setOut(new PrintStream(new JTextAreaOutputStream(outArea)));
        System.setErr(new PrintStream(new JTextAreaOutputStream(errArea)));
    }

    public static void main(String[] args) {
        new StdErrOutWindows();
        System.out.println("test to out");
        try {
            throw new Exception("Test exception");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class JTextAreaOutputStream extends OutputStream {
        JTextArea ta;

        public JTextAreaOutputStream(JTextArea t) {
            super();
            ta = t;
        }

        public void write(int i) {
            ta.append(Character.toString((char) i));
        }

        public void write(char[] buf, int off, int len) {
            String s = new String(buf, off, len);
            ta.append(s);
        }

    }

}