Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Writing directly to a JTextArea (See related posts)

Writer implementation which provides a means for writing directly to a JTextArea.

   1  
   2  import java.io.Writer;
   3  import java.io.IOException;
   4  
   5  import javax.swing.JTextArea;
   6  
   7  /** A implementation of the java.io.Writer class which facilitates writing to a JTextArea via a stream.
   8      
   9      <p><b>Note:</b> There appears to be bug in the Macintosh implementation of 
  10      the JDK 1.1 where a PrintWriter writing to this class will not include the 
  11      correct line feeds for display in a JTextArea.  There is a simple test of
  12      the "java.version" system property which, if it starts with the String "1.1"
  13      will cause newlines to be written each time the buffer is flushed.</p>
  14      
  15      @author Anthony Eden
  16  */
  17  
  18  public class JTextAreaWriter extends Writer{
  19      
  20      private boolean closed = false;
  21      private JTextArea textArea;
  22      private StringBuffer buffer;
  23  
  24      /** Constructor.
  25      
  26          @param textArea The JTextArea to write to.
  27      */
  28  
  29      public JTextAreaWriter(JTextArea textArea){
  30          setTextArea(textArea);
  31      }
  32      
  33      /** Set the JTextArea to write to.
  34      
  35          @param textArea The JTextArea
  36      */
  37      
  38      public void setTextArea(JTextArea textArea){
  39          if(textArea == null){
  40              throw new IllegalArgumentException("The text area must not be null.");
  41          }
  42          this.textArea = textArea;
  43      }
  44      
  45      /** Close the stream. */
  46  
  47      public void close(){
  48          closed = true;
  49      }
  50      
  51      /** Flush the data that is currently in the buffer.
  52      
  53          @throws IOException
  54      */
  55      
  56      public void flush() throws IOException{
  57          if(closed){
  58              throw new IOException("The stream is not open.");
  59          }
  60          // the newline character should not be necessary.  The PrintWriter
  61          // should autmatically put the newline, but it doesn't seem to work
  62          textArea.append(getBuffer().toString());
  63          if(System.getProperty("java.version").startsWith("1.1")){
  64              textArea.append("\n");
  65          }
  66          textArea.setCaretPosition(textArea.getDocument().getLength());
  67          buffer = null;
  68      }
  69      
  70      /** Write the given character array to the output stream.
  71      
  72          @param charArray The character array
  73          @throws IOException
  74      */
  75      
  76      public void write(char[] charArray) throws IOException{
  77          write(charArray, 0, charArray.length);
  78      }
  79      
  80      /** Write the given character array to the output stream beginning from
  81          the given offset and proceeding to until the given length is reached.
  82      
  83          @param charArray The character array
  84          @param offset The start offset
  85          @param length The length to write
  86          @throws IOException
  87      */
  88      
  89      public void write(char[] charArray, int offset, int length) throws IOException{
  90          if(closed){
  91              throw new IOException("The stream is not open.");
  92          }
  93          getBuffer().append(charArray, offset, length);
  94      }
  95      
  96      /** Write the given character to the output stream.
  97      
  98          @param c The character
  99          @throws IOException
 100      */
 101      
 102      public void write(int c) throws IOException{
 103          if(closed){
 104              throw new IOException("The stream is not open.");
 105          }
 106          getBuffer().append((char)c);
 107      }
 108      
 109      /** Write the given String to the output stream.
 110      
 111          @param string The String
 112          @throws IOException
 113      */
 114      
 115      public void write(String string) throws IOException{
 116          if(closed){
 117              throw new IOException("The stream is not open.");
 118          }
 119          getBuffer().append(string);
 120      }
 121      
 122      /** Write the given String to the output stream beginning from the given offset 
 123          and proceeding to until the given length is reached.
 124      
 125          @param string The String
 126          @param offset The start offset
 127          @param length The length to write
 128          @throws IOException
 129      */
 130      
 131      public void write(String string, int offset, int length) throws IOException{
 132          if(closed){
 133              throw new IOException("The stream is not open.");
 134          }
 135          getBuffer().append(string.substring(offset, length));
 136      }
 137      
 138      // protected methods
 139      
 140      /** Get the StringBuffer which holds the data prior to writing via
 141          a call to the <code>flush()
method. This method should
never return null.

@return A StringBuffer
*/

protected StringBuffer getBuffer(){
if(buffer == null){
buffer = new StringBuffer();
}
return buffer;
}

}

You need to create an account or log in to post comments to this site.


Click here to browse all 5545 code snippets

Related Posts