O'Reilly logo

Java I/O by Elliotte Rusty Harold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Graphical User Interface for Output Streams

As a useful example, I’m going to show a subclass of java.awt.TextArea that can be connected to an output stream. As data is written onto the stream, it is appended to the text area in the default character set (generally ISO Latin-1). (This isn’t ideal. Since text areas contain text, a writer would be a better source for this data; in later chapters I’ll expand on this class to use a writer instead. For now this makes a neat example.) This subclass is shown in Example 2.4.

The actual output stream is contained in an inner class inside the StreamedTextArea class. Each StreamedTextArea component contains a TextAreaOutputStream object in its theOutput field. Client programmers access this object via the getOutputStream() method of the StreamedTextArea class. The StreamedTextArea class has five overloaded constructors that imitate the five constructors in the java.awt.TextArea class, each taking a different combination of text, rows, columns, and scrollbar information. The first four constructors merely pass their arguments and suitable defaults to the most general fifth constructor using this(). The fifth constructor calls the most general superclass constructor, then calls setEditable(false) to ensure that the user doesn’t change the text while output is streaming into it.

I’ve chosen not to override any methods in the TextArea superclass. However, you might want to do so if you feel a need to change the normal abilities of a text area. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required