Writing to a JTextArea

Download: jar file

If we use System.out.println(...) in a console program, the output appears in the console window. To run this program as an applet or a windows application, the output needs to go to a GUI component, such as a JTextArea.

The easiest way is to subclass OutputStream and override just the methods needed to get characters copied into the JTextArea.

We choose to subclass PrintSream and override methods println(String) and print(String), along with several other similar methods. Then we could write to an instance of the subclass and have the output go directly to the JTextArea. We could also redirect System.out using the System.setOut method.

Demonstration

The jar file was generated by

    jar cef TextDemo TextDemo.jar *.java *.class

Redirecting System.out


    void run()
    {
	System.setOut(out);
	System.out.println("Hello, World!");
	for (int i=1; i<=10; i++) System.out.printf("%d\n",i);
	System.out.println("Goodbye, all");
    }
}

TextDemo.java


01: import java.io.*;
02: import java.net.*;
03: import java.awt.EventQueue;
04: import javax.swing.*;
05: 
06: 
07: class Process {
08:     JTextArea area;
09:     TextAreaPrintStream out;
10: 
11:     Process()
12:     {
13:         area = new JTextArea(9,25);
14:         out = new TextAreaPrintStream(area,System.out);
15:     }
16: 
17:     void run()
18:     {
19:         out.println("Hello, World!");
20:         for (int i=1; i<=10; i++) out.printf("%d\n",i);
21:         out.println("Goodbye, all");
22:     }
23: }
24: 
25: public class TextDemo extends JApplet {
26: 
27:     public void init()
28:     {
29:         EventQueue.invokeLater(new Runnable() {
30:             public void run()
31:             {
32:                 Process proc = new Process();
33:                 add(new JScrollPane(proc.area));
34:                 proc.run();
35:             }
36:         });
37:     }
38:     
39:     public static void main(String[] args)
40:     {
41:         String filename;
42: 
43:         final Process proc = new Process();
44: 
45:         EventQueue.invokeLater(new Runnable() {
46:             public void run()
47:             {
48:                 JFrame frame = new JFrame("TextAreaPrintStream Demo");
49:                 frame.add(new JScrollPane(proc.area));
50:                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51:                 frame.pack();
52:                 //frame.setSize(640,200);
53:                 frame.setVisible(true);
54:                 proc.run();
55:             }
56:         });
57:     }
58: }


TextAreaPrintStream.java


01: /**
02:  * Class TextAreaPrintStream
03:  * extends PrintStream.
04:  * A custom made PrintStream which overrides methods println(String)
05:  * and print(String).
06:  * Thus, when the out stream is set as this PrintStream (with System.setOut
07:  * method), all calls to System.out.println(String) or System.out.print(String)
08:  * will result in an output stream of characters in the JTextArea given as an
09:  * argument of the constructor of the class.
10:  **/
11: 
12: import java.io.*;
13: import javax.swing.*;
14: 
15: public class TextAreaPrintStream extends PrintStream {
16: 
17:     //The JTextArea to wich the output stream will be redirected.
18:     private JTextArea textArea;
19: 
20: 
21:     /**
22:      * Method TextAreaPrintStream
23:      * The constructor of the class.
24:      * @param the JTextArea to wich the output stream will be redirected.
25:      * @param a standard output stream (needed by super method)
26:      **/
27:     public TextAreaPrintStream(JTextArea area, OutputStream out) {
28:         super(out);
29:         textArea = area;
30:     }
31: 
32:     /**
33:      * Method println
34:      * @param the String to be output in the JTextArea textArea (private
35:      * attribute of the class).
36:      * After having printed such a String, prints a new line.
37:      **/
38:     public void println(String string) {
39:         textArea.append(string+"\n");
40:     }
41: 
42:     public void println() {
43:         textArea.append("\n");
44:     }
45: 
46:     public void println(Object obj) {
47:         textArea.append(obj.toString()+"\n");
48:     }
49: 
50: 
51: 
52:     /**
53:      * Method print
54:      * @param the String to be output in the JTextArea textArea (private
55:      * attribute of the class).
56:      **/
57:     public void print(String string) {
58:         textArea.append(string);
59:     }
60: 
61:     public void print(Object obj) {
62:         textArea.append(obj.toString());
63:     }
64: }


Maintained by John Loomis, updated Wed Mar 26 14:25:19 2008