1   package kerhoswing;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.io.PrintStream;
6   
7   import javax.swing.JTextArea;
8   
9   /**
10   * Simple way to "print" to a JTextArea; just say
11   * PrintStream out = new PrintStream(new TextAreaOutputStream(myTextArea));
12   * Then out.println() et all will all appear in the TextArea.
13   * Source: http://javacook.darwinsys.com/new_recipes/14.9betterTextToTextArea.jsp
14   */
15  public final class TextAreaOutputStream extends OutputStream {
16  
17      private final JTextArea textArea;
18      // private final StringBuilder sb = new StringBuilder();
19  
20      /**
21       * @param textArea area where to write
22       * @example
23       * <pre name="test">
24       * #import java.io.*;
25       * #import javax.swing.*;
26       *   JTextArea text = new JTextArea();
27       *   PrintStream tw = new PrintStream(new TextAreaOutputStream(text));
28       *   tw.print("Hello");
29       *   tw.print(" ");
30       *   tw.print("world!");
31       *   tw.flush();
32       *   text.getText() === "Hello world!";
33       *   text.setText("");
34       *   tw.println("Hello");
35       *   tw.println("world!");
36       *   text.getText() =R= "Hello\\r?\\nworld!\\r?\\n";
37       * </pre>
38       */
39      public TextAreaOutputStream(final JTextArea textArea) {
40          this.textArea = textArea;
41      }
42  
43      @Override
44      public void flush(){ 
45          //textArea.append(sb.toString());
46          //sb.setLength(0);
47      }
48      
49      @Override
50      public void close(){ }
51  
52      @Override
53      public void write(int b) throws IOException {
54          /*
55          if (b == '\r')
56              return;
57          
58          if (b == '\n') {
59              textArea.append(sb.toString()+"\n");
60              sb.setLength(0);
61              return;
62          }
63          
64          sb.append((char)b);
65          */
66          //textArea.append(Character.toString((char)b));
67          write(new byte[] {(byte) b}, 0, 1);
68      }
69   
70      @Override
71      public void write(byte b[], int off, int len) throws IOException {  
72          textArea.append(new String(b, off, len));  
73      }    
74      
75      
76      /**
77       * Factory method for creating a PrintStream to print to selected TextArea
78       * @param textArea area where to print
79       * @return created PrintStream ready to print to TextArea
80       * @example
81       * <pre name="test">
82       * #import java.io.*;
83       * #import javax.swing.*;
84       *   JTextArea text = new JTextArea();
85       *   PrintStream tw = TextAreaOutputStream.getTextPrintStream(text);
86       *   tw.print("Hyvää"); // skandit toimi
87       *   tw.print(" ");
88       *   tw.print("päivää!");
89       *   text.getText() === "Hyvää päivää!";
90       *   text.setText("");
91       *   tw.print("ä");
92       *   text.getText() === "ä"; 
93       * </pre>
94       */
95      public static PrintStream getTextPrintStream(JTextArea textArea) {
96              return new PrintStream(new TextAreaOutputStream(textArea)); //,true,"ISO-8859-1");
97      }
98      
99  }
100 
101