001/**
002*   Please feel free to use any fragment of the code in this file that you need
003*   in your own work. As far as I am concerned, it's in the public domain. No
004*   permission is necessary or required. Credit is always appreciated if you
005*   use a large chunk or base a significant product on one of my examples,
006*   but that's not required either.
007*
008*   This code is distributed in the hope that it will be useful,
009*   but WITHOUT ANY WARRANTY; without even the implied warranty of
010*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011*
012*      --- Joseph A. Huwaldt
013**/
014package jahuwaldt.swing;
015
016import javax.swing.JFrame;
017import javax.swing.JScrollPane;
018import javax.swing.JTextArea;
019import java.io.OutputStream;
020import java.io.PrintWriter;
021
022/**
023*  A window that displays a text area that can be written
024*  to using an OutputStream.
025*
026*  <p>  Modified by:  Joseph A. Huwaldt    </p>
027*
028*  @author    Joseph A. Huwaldt    Date:  March 15, 2003
029*  @version   September 16, 2012
030**/
031@SuppressWarnings("serial")
032public class StreamedTextViewer extends JFrame {
033
034    private JStreamedTextArea textArea = new JStreamedTextArea();
035
036        /**
037        *  Construct a streamed text area window with the specified title.
038        **/
039    public StreamedTextViewer(String title) {
040        super(title);
041        textArea.setEditable(false);
042                textArea.setDragEnabled(true);
043                JScrollPane scrollPane = new JScrollPane(textArea);
044                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
045                scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
046        getContentPane().add(scrollPane);
047    }
048
049        /**
050        *  Return an output stream to the text area in this frame.  Text
051        *  streamed to this output stream will be displayed in this frame.
052        **/
053        public OutputStream getOutputStream() {
054                return textArea.getOutputStream();
055        }
056        
057        /**
058        *  Return the text area used to display the output stream.
059        **/
060        public JTextArea getTextArea() {
061                return textArea;
062        }
063        
064    public static void main(String[] args) {
065        StreamedTextViewer frame = new StreamedTextViewer("Test");
066                OutputStream stream = frame.getOutputStream();
067                
068                PrintWriter writer = new PrintWriter(stream, true);
069                writer.println("This life is a test.  This life is only a test.");
070                writer.println("If this had been a real life, you would have been");
071                writer.println("told where to go and what to do.");
072                
073        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
074        frame.setVisible(true);
075    }
076}
077
078
079
080
081