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 java.io.OutputStream;
017import java.io.PrintWriter;
018import javax.swing.JFrame;
019import javax.swing.JScrollPane;
020import javax.swing.JTextArea;
021
022/**
023 * A window that displays a text area that can be written to using an OutputStream.
024 *
025 * <p> Modified by: Joseph A. Huwaldt    </p>
026 *
027 * @author Joseph A. Huwaldt Date: March 15, 2003
028 * @version February 23, 2025
029 */
030@SuppressWarnings("serial")
031public class StreamedTextViewer extends JFrame {
032
033    private final JStreamedTextArea textArea = new JStreamedTextArea();
034
035    /**
036     * Construct a streamed text area window with the specified title.
037     *
038     * @param title The title for the streamed text area window.
039     */
040    @SuppressWarnings("OverridableMethodCallInConstructor")
041    public StreamedTextViewer(String title) {
042        super(title);
043        textArea.setEditable(false);
044        textArea.setDragEnabled(true);
045        JScrollPane scrollPane = new JScrollPane(textArea);
046        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
047        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
048        getContentPane().add(scrollPane);
049    }
050
051    /**
052     * Return an output stream to the text area in this frame. Text streamed to this
053     * output stream will be displayed in this frame.
054     *
055     * @return The output stream to the text area in this frame.
056     */
057    public OutputStream getOutputStream() {
058        return textArea.getOutputStream();
059    }
060
061    /**
062     * Return the text area used to display the output stream.
063     *
064     * @return The text area used to display the output stream.
065     */
066    public JTextArea getTextArea() {
067        return textArea;
068    }
069
070    public static void main(String[] args) {
071        StreamedTextViewer frame = new StreamedTextViewer("Test");
072        OutputStream stream = frame.getOutputStream();
073
074        PrintWriter writer = new PrintWriter(stream, true);
075        writer.println("This life is a test.  This life is only a test.");
076        writer.println("If this had been a real life, you would have been");
077        writer.println("told where to go and what to do.");
078
079        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
080        frame.setVisible(true);
081    }
082}