public class PanelOutputStream
extends java.io.OutputStream
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.
Constructor and Description |
---|
PanelOutputStream()
Creates a new panel output stream.
|
Modifier and Type | Method and Description |
---|---|
javax.swing.JPanel |
getPanel()
Gets the associated output panel.
|
javax.swing.JTextArea |
getTextArea()
Gets the associated text area.
|
static void |
main(java.lang.String[] argv)
Tests this class.
|
void |
write(byte[] b)
Writes
b.length bytes from the specified byte array
to this output stream. |
void |
write(byte[] b,
int off,
int len)
Writes
len bytes from the specified byte array
starting at offset off to this output stream. |
void |
write(int b)
Writes the specified byte to this output stream.
|
public javax.swing.JPanel getPanel()
public javax.swing.JTextArea getTextArea()
public void write(int b) throws java.io.IOException
java.io.OutputStream.write(int)
.write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b) throws java.io.IOException
b.length
bytes from the specified byte array
to this output stream. See the general contract for
java.io.OutputStream.write(byte[])
.write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
len
bytes from the specified byte array
starting at offset off
to this output stream. See
the general contract for
java.io.OutputStream.write(byte[],int,int)
.write
in class java.io.OutputStream
java.io.IOException
public static void main(java.lang.String[] argv)
argv
- the array of command line parameters.