Class TextPaneScrollPane

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, ScrollPaneConstants
Direct Known Subclasses:
FeedbackPane, JsonRawPane, LogPane

public class TextPaneScrollPane extends JScrollPane
A JScrollPane that owns a non-editable JTextPane using a StyledDocument and a collection of convenience styles for terminal / status-like output.

This class is also written to behave well under the Nimbus look & feel: the scroll pane and its viewport are opaque and painted with the configured background color, while the embedded JTextPane is non-opaque with a transparent background. This avoids Nimbus painting only behind the text while leaving the rest of the area white.

Typical usage:

 TextPaneScrollPane pane = new TextPaneScrollPane("Feedback", Color.BLACK);
 pane.append("Hello world\n", TextPaneScrollPane.CYAN_TERMINAL);
 
See Also:
  • Field Details

    • BLUE_M_10_B

      public static final SimpleAttributeSet BLUE_M_10_B
      Small blue monospaced style on a light blue background.
    • RED_TERMINAL

      public static final SimpleAttributeSet RED_TERMINAL
      Red monospaced terminal style on a transparent background.
    • YELLOW_TERMINAL

      public static final SimpleAttributeSet YELLOW_TERMINAL
      Yellow monospaced terminal style on a transparent background.
    • CYAN_TERMINAL

      public static final SimpleAttributeSet CYAN_TERMINAL
      Cyan monospaced terminal style on a transparent background.
    • BLUE_TERMINAL

      public static final SimpleAttributeSet BLUE_TERMINAL
      Blue monospaced terminal style.
    • GREEN_TERMINAL

      public static final SimpleAttributeSet GREEN_TERMINAL
      Green monospaced terminal style on a transparent background.
    • BLUE_SS_12_B

      public static final SimpleAttributeSet BLUE_SS_12_B
      Blue bold sans-serif on a light background.
    • BLUE_SS_12_P

      public static final SimpleAttributeSet BLUE_SS_12_P
      Blue plain sans-serif.
    • GREEN_SS_12_B

      public static final SimpleAttributeSet GREEN_SS_12_B
      Dark green bold sans-serif on a wheat background.
    • RED_SS_12_P

      public static final SimpleAttributeSet RED_SS_12_P
      Red plain sans-serif.
    • GREEN_SS_12_P

      public static final SimpleAttributeSet GREEN_SS_12_P
      Dark green plain sans-serif on a wheat background.
    • BLACK_SS_12_P

      public static final SimpleAttributeSet BLACK_SS_12_P
      Black plain sans-serif.
    • BLUE_SS_10_B

      public static final SimpleAttributeSet BLUE_SS_10_B
      Small blue bold sans-serif on a light background.
    • RED_SS_10_P

      public static final SimpleAttributeSet RED_SS_10_P
      Small red plain sans-serif.
    • GREEN_SS_10_P

      public static final SimpleAttributeSet GREEN_SS_10_P
      Small green plain sans-serif on a yellow background.
    • BLACK_SS_10_P

      public static final SimpleAttributeSet BLACK_SS_10_P
      Small black plain sans-serif.
    • textPane

      protected JTextPane textPane
      The non-editable text pane hosted by this scroll pane.
    • document

      protected StyledDocument document
      The styled document backing the text pane.
    • defaultStyle

      protected SimpleAttributeSet defaultStyle
      Default style used when no explicit style is supplied.
  • Constructor Details

    • TextPaneScrollPane

      public TextPaneScrollPane()
      Creates a new TextPaneScrollPane with a white background and no border label.
    • TextPaneScrollPane

      public TextPaneScrollPane(Color bgColor)
      Creates a new TextPaneScrollPane with no border label.
      Parameters:
      bgColor - the background color to use; if null, white is used
    • TextPaneScrollPane

      public TextPaneScrollPane(String label, Color bgColor)
      Creates a new TextPaneScrollPane, optionally with a labeled border.
      Parameters:
      label - optional border label; if null, no labeled border is installed
      bgColor - the background color to use; if null, white is used
  • Method Details

    • createStyle

      public static SimpleAttributeSet createStyle(Color fg, String fontFamily, int fontSize, boolean italic, boolean bold)
      Creates a style with the given characteristics and a default white background and spacing.
      Parameters:
      fg - the foreground color
      fontFamily - the font family to use
      fontSize - the font size to use
      italic - true for italic text
      bold - true for bold text
      Returns:
      the created style
    • createStyle

      public static SimpleAttributeSet createStyle(Color fg, Color bg, String fontFamily, int fontSize, boolean italic, boolean bold)
      Creates a style with the given foreground and background colors and default spacing.
      Parameters:
      fg - the foreground color
      bg - the background color
      fontFamily - the font family to use
      fontSize - the font size to use
      italic - true for italic text
      bold - true for bold text
      Returns:
      the created style
    • createStyle

      public static SimpleAttributeSet createStyle(Color fg, Color bg, String fontFamily, int fontSize, boolean italic, boolean bold, boolean underline, int spaceAbove, int spaceBelow)
      Creates a fully specified style.
      Parameters:
      fg - the foreground color
      bg - the background color
      fontFamily - the font family to use
      fontSize - the font size to use
      italic - true for italic text
      bold - true for bold text
      underline - true to underline
      spaceAbove - additional space above the paragraph, in points
      spaceBelow - additional space below the paragraph, in points
      Returns:
      the created style
    • refresh

      public void refresh()
      Forces a repaint of the underlying text pane.
    • append

      public void append(String text)
      Appends plain text using the current defaultStyle.
      Parameters:
      text - the text to append; null is ignored
    • append

      public void append(String text, AttributeSet style)
      Appends text using a specific style.
      Parameters:
      text - the text to append; null is ignored
      style - the style to use (for example CYAN_TERMINAL)
    • append

      public void append(String text, AttributeSet style, boolean writeTime)
      Appends text using a specific style and optional timestamp.
      Parameters:
      text - the text to append; null is ignored
      style - the style to use
      writeTime - true to prepend a timestamp using BLUE_M_10_B
    • clear

      public void clear()
      Clears all text and resets the caret position to the beginning.
    • getDefaultStyle

      public SimpleAttributeSet getDefaultStyle()
      Returns the current default style used by append(String).
      Returns:
      the default style
    • setDefaultStyle

      public void setDefaultStyle(SimpleAttributeSet defaultStyle)
      Sets the default style used by append(String).
      Parameters:
      defaultStyle - the new default style