Class PanelOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
The PanelOutputStream
class extends
java.io.OutputStream
to display output in a Swing
JPanel
. The most common use is to create a
PanelOutputStream
to be used in combination with a
java.io.PrintStream
object:
PanelOutputStream panelStream = new PanelOutputStream(); PrintStream printStream = new PrintStream (panelStream, true); JPanel panel = panelStream.getPanel(); ... printStream.println ("Hello, world!");
The output stream has a special method getPanel()
that
retrieves a javax.swing.JPanel
object that may be used
to display the output. The retrieved JPanel
is simply
a Swing text area inside a scrollable pane. The text area is set
to non-editable.
In general, as output is appended to the text area, the scroll panel scrolls to the bottom so that the new output is visible. To explicitly set the caret position of the text area, you can do something like this:
PanelOutputStream panelStream = new PanelOutputStream(); JTextArea textArea = panelStream.getTextArea(); ... textArea.setCaretPosition (0);
The text area is also useful for setting specific fonts, for example a fixed space font rather than the default proportional space font.
- Since:
- 3.1.7
- Author:
- Peter Hollemans
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetPanel()
Gets the associated output panel.Gets the associated text area.static void
Tests this class.void
write
(byte[] b) Writesb.length
bytes from the specified byte array to this output stream.void
write
(byte[] b, int off, int len) Writeslen
bytes from the specified byte array starting at offsetoff
to this output stream.void
write
(int b) Writes the specified byte to this output stream.Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream
-
Constructor Details
-
PanelOutputStream
public PanelOutputStream()Creates a new panel output stream.
-
-
Method Details
-
getPanel
Gets the associated output panel. -
getTextArea
Gets the associated text area. -
write
Writes the specified byte to this output stream. See the general contract forjava.io.OutputStream.write(int)
.- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
Writesb.length
bytes from the specified byte array to this output stream. See the general contract forjava.io.OutputStream.write(byte[])
.- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
Writeslen
bytes from the specified byte array starting at offsetoff
to this output stream. See the general contract forjava.io.OutputStream.write(byte[],int,int)
.- Overrides:
write
in classOutputStream
- Throws:
IOException
-
main
Tests this class.- Parameters:
argv
- the array of command line parameters.
-