Class SimpleJTextPane

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Appendable, Accessible, Scrollable

public class SimpleJTextPane extends JTextPane implements Appendable
A JTextPane with convenience methods and a small number of styles. It uses a default font, but lets you make the font bold, italic, or appear in various colors. This is useful for displaying source code or similar outputs where keywords might appear in a different font or color from variables, or where key/value pairs show keys in bold fonts and values in normal fonts.

The method setSwingSafe(boolean) can be used to configure this component so that it can be modified from any thread, not just the event dispatch thread. Allowing the component to be modified from any thread is useful for applications with long running times that change the contents of this component.

This class makes use of the static method SwingUtilities.invokeLater(Runnable). One should avoid the use of synchronized methods that call methods in this class when those synchronized methods might be called from tasks waiting on the AWT event dispatch queue, as there is a possibility of deadlock: If for some class methods m1 and m2 are synchronized and call one of the methods in this class, and m1 is called, a call to SwingUtilities.invokeLater(Runnable) may process other entries on its event queue first, causing m2 to be called, but m2 will wait until m1 returns, which cannot occur until m2 returns.

See Also:
  • Constructor Details

    • SimpleJTextPane

      public SimpleJTextPane()
      Default Constructor.
    • SimpleJTextPane

      public SimpleJTextPane(StyledDocument doc)
      Constructor with a specific underlying document.
      Parameters:
      doc - the document for the component's model.
  • Method Details

    • saveAttributeState

      public SimpleJTextPane.State saveAttributeState()
      Save an attribute state so that it can be restored later. The value returned may be used in a try-with-resources block so that the state will be automatically restored when the block exits for whatever reason.

      This method is thread safe.

      Returns:
      an object storing the attribute state
    • perform

      public void perform(Consumer<SimpleJTextPane> c)
      Run a sequence of operations atomically. The argument c is a lambda express that will be called with its single argument set to this SimpleJTextPane. Because this method is synchronized, the operations the lambda expression performs will not be interleaved with synchronized calls from other threads. Alternatively, one can implement the consumer's accept method instead of using a lambda expression.

      This method is thread safe.

      Parameters:
      c - the consumer
    • isBold

      public boolean isBold()
      Determine if the font weight is appropriate for bold text.

      This method is thread safe.

      Returns:
      true if newly inserted text will be bold; false otherwise
    • setBold

      public void setBold(boolean value)
      Set the font weight for subsequently inserted text to bold or normal.

      This method is thread safe.

      Parameters:
      value - true if the font is bold; false if it's weight is normal.
    • isItalic

      public boolean isItalic()
      Determine if newly inserted text will be in italics.

      This method is thread safe.

      Returns:
      true if newly inserted text will be italics; false otherwise
    • setItalic

      public void setItalic(boolean value)
      Set the font slant for subsequently inserted text.

      This method is thread safe.

      Parameters:
      value - true if the font is italic; false if not.
    • getTextForeground

      public Color getTextForeground()
      Get the color of subsequently inserted or appended text. The initial value is the foreground color of this pane when it is created (if not set, it inherits the color for this pane's parent).

      This method is thread safe.

      Returns:
      The color that will be used for new text.
    • setTextForeground

      public void setTextForeground(Color fg)
      Set the text color for new text.

      This method is thread safe.

      Parameters:
      fg - the color for subsequently inserted or appended text.
    • getTextBackground

      public Color getTextBackground()
      Get the background color for subsequently inserted or appended text. The initial value is the background color of this pane when it is created (if not set, it inherits the color for this pane's parent).

      This method is thread safe.

      Returns:
      The color that will be used for the background for new text.
    • setTextBackground

      public void setTextBackground(Color bg)
      Set the background color for new text.

      This method is thread safe.

      Parameters:
      bg - the background color for subsequently inserted or appended text.
    • setSwingSafe

      public void setSwingSafe(boolean flag)
      Set whether or not this component is thread-safe.
      Parameters:
      flag - true if this component is thread safe; false if modifications to this component must be made from the event dispatch thread.
    • isSwingSafe

      public boolean isSwingSafe()
      Test if this component is thread safe.

      This method is thread safe.

      Returns:
      true if this component is thread-safe; false if changes must be made from the event dispatch thread
    • setDocument

      public void setDocument(Document doc)
      Overrides:
      setDocument in class JTextPane
    • setStyledDocument

      public void setStyledDocument(StyledDocument doc)
      Overrides:
      setStyledDocument in class JTextPane
    • insertString

      public void insertString(int off, String str) throws BadLocationException
      Insert text into the component's document.

      This method is thread safe if when isSwingSafe() returns true.

      Parameters:
      off - the offset into the document at which the new text should be inserted
      str - the text to insert.
      Throws:
      BadLocationException - attempted to insert text at a non-existent location
    • getLength

      public int getLength()
      Get the number of characters in this text pane.

      This method is thread safe if when isSwingSafe() returns true.

      Returns:
      the number of characters
    • appendString

      public void appendString(String str)
      Insert text at the end of the component's document.

      This method is thread safe.

      Parameters:
      str - the text to insert.
    • append

      public Appendable append(char c)
      Append a character to the end of the component's document.

      This method is thread safe.

      Specified by:
      append in interface Appendable
      Parameters:
      c - the character to append
      Returns:
      the current object cast as an Appendable
    • append

      public Appendable append(CharSequence csq)
      Append text to the end of the component's document.

      This method is thread safe.

      Specified by:
      append in interface Appendable
      Parameters:
      csq - the text to append
      Returns:
      the current object cast as an Appendable
    • append

      public Appendable append(CharSequence csq, int start, int end)
      Append text to the end of the component's document.

      This method is thread safe.

      Specified by:
      append in interface Appendable
      Parameters:
      csq - a CharSequence containing the text to append
      start - the offset to the start of the text in csq
      end - The position just past the end of the text to in csq to append
      Returns:
      the current object cast as an Appendable
    • remove

      public void remove(int start, int length) throws BadLocationException
      Remove text from the document the component displays.

      This method is thread safe if when isSwingSafe() returns true.

      Parameters:
      start - the starting offset into the document for the text to be removed..
      length - the length of the text to be removed.
      Throws:
      BadLocationException - the arguments cover a range of text that is not part of the document.
    • clear

      public void clear()
      Remove all the text from a document.

      This method is thread safe.