JTextPane: getDocument() : JTextPane « javax.swing « Java by API






JTextPane: getDocument()

 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class MainClass extends JFrame {
  private JTextPane textPane = new JTextPane();

  public MainClass() {
    super();
    setSize(300, 200);

    textPane.setFont(new Font("Serif", Font.PLAIN, 24));

    // create some handy attribute sets
    SimpleAttributeSet red = new SimpleAttributeSet();
    StyleConstants.setForeground(red, Color.red);
    StyleConstants.setBold(red, true);
    SimpleAttributeSet blue = new SimpleAttributeSet();
    StyleConstants.setForeground(blue, Color.blue);
    SimpleAttributeSet italic = new SimpleAttributeSet();
    StyleConstants.setItalic(italic, true);
    StyleConstants.setForeground(italic, Color.orange);

    // add the text
    append("NULL ", null);
    append("Blue", blue);
    append("italic", italic);
    append("red", red);

    Container content = getContentPane();
    content.add(new JScrollPane(textPane), BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  protected void append(String s, AttributeSet attributes) {
    Document d = textPane.getDocument();
    try {
      d.insertString(d.getLength(), s, attributes);
    } catch (BadLocationException ble) {
    }
  }

  public static void main(String[] args) {
    new MainClass().setVisible(true);
  }
}

           
         
  








Related examples in the same category

1.JTextPane: getStyledDocument()
2.JTextPane: insertComponent(Component c)
3.JTextPane: insertIcon(Icon g)
4.JTextPane: replaceSelection(String content)
5.JTextPane: setCaretPosition(int position)
6.JTextPane.setCharacterAttributes(AttributeSet attr, boolean replace)
7.JTextPane: setFont(Font font)
8.JTextPane: setLogicalStyle(Style s)
9.JTextPane: setParagraphAttributes(AttributeSet attr, boolean replace)