Sharing Styles Between JTextPanes - Java Swing

Java examples for Swing:JTextPane

Description

Sharing Styles Between JTextPanes

Demo Code

import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class Main {
  public static void main(String[] argv) {
    JTextPane c1 = new JTextPane();
    JTextPane c2 = new JTextPane();

    // Create new styled documents with the same style context
    StyleContext styleContext = new StyleContext();
    c1.setDocument(new DefaultStyledDocument(styleContext));
    c2.setDocument(new DefaultStyledDocument(styleContext));

    // Create a new style with one text pane
    Style style = c1.addStyle("style name", null);
    StyleConstants.setForeground(style, Color.red);

    // Modify an existing style using the other text pane
    style = c2.getStyle("style name");
    StyleConstants.setBold(style, true);
  }/*from  w w w. j a v a2 s .c  om*/
}

Related Tutorials