Java JTextPane

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JTextPane

JTextPane is a subclass of JEditorPane class. JTextPane is used for styled document with embedded images and components. It is text component that can be marked up with attributes that are represented graphically. JTextPane uses a DefaultStyledDocument as its default model.

Constructors

Constructor Description
JTextPane() It creates a new JTextPane.
JtextPane(StyledDocument doc) It creates a new JTextPane, with a specified document model.

Useful Methods

Modifier and Type Method Description
Style addStyle(String nm, Style parent) It adds a new style into the logical style hierarchy.
AttributeSet getCharacterAttributes() It fetches the character attributes in effect at the current location of the caret, or null.
StyledDocument getStyledDocument() It fetches the model associated with the editor.
void setDocument(Document doc) It associates the editor with a text document.
void setCharacterAttributes(AttributeSet attr, boolean replace) It applies the given attributes to character content.
void removeStyle(String nm) It removes a named non-null style previously added to the document.
void setEditorKit(EditorKit kit) It sets the currently installed kit for handling content.
void setStyledDocument(StyledDocument doc) It associates the editor with a text document.

JTextPane Example

  1. import java.awt.BorderLayout;  
  2. import java.awt.Color;  
  3. import java.awt.Container;  
  4. import javax.swing.JFrame;  
  5. import javax.swing.JScrollPane;  
  6. import javax.swing.JTextPane;  
  7. import javax.swing.text.BadLocationException;  
  8. import javax.swing.text.Document;  
  9. import javax.swing.text.SimpleAttributeSet;  
  10. import javax.swing.text.StyleConstants;  
  11. public class JTextPaneExample {  
  12.     public static void main(String args[]) throws BadLocationException {  
  13.         JFrame frame = new JFrame("JTextPane Example");  
  14.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  15.         Container cp = frame.getContentPane();  
  16.         JTextPane pane = new JTextPane();  
  17.         SimpleAttributeSet attributeSet = new SimpleAttributeSet();  
  18.         StyleConstants.setBold(attributeSet, true);  
  19.   
  20.         // Set the attributes before adding text  
  21.         pane.setCharacterAttributes(attributeSet, true);  
  22.         pane.setText("Welcome");  
  23.   
  24.         attributeSet = new SimpleAttributeSet();  
  25.         StyleConstants.setItalic(attributeSet, true);  
  26.         StyleConstants.setForeground(attributeSet, Color.red);  
  27.         StyleConstants.setBackground(attributeSet, Color.blue);  
  28.   
  29.         Document doc = pane.getStyledDocument();  
  30.         doc.insertString(doc.getLength(), "To Java ", attributeSet);  
  31.   
  32.         attributeSet = new SimpleAttributeSet();  
  33.         doc.insertString(doc.getLength(), "World", attributeSet);  
  34.   
  35.         JScrollPane scrollPane = new JScrollPane(pane);  
  36.         cp.add(scrollPane, BorderLayout.CENTER);  
  37.   
  38.         frame.setSize(400300);  
  39.         frame.setVisible(true);  
  40.       }  
  41. }  

Output

Java JTextpane