Java JTextPane insertStyledString(JTextPane textPane, String text, SimpleAttributeSet attrSet, Color background, Color foreground, String fontFamily, int fontSize, boolean bold, boolean italic, boolean underline, boolean strikeThrough, Boolean superscript)

Here you can find the source of insertStyledString(JTextPane textPane, String text, SimpleAttributeSet attrSet, Color background, Color foreground, String fontFamily, int fontSize, boolean bold, boolean italic, boolean underline, boolean strikeThrough, Boolean superscript)

Description

insert Styled String

License

LGPL

Declaration

public static SimpleAttributeSet insertStyledString(JTextPane textPane, String text, SimpleAttributeSet attrSet,
        Color background, Color foreground, String fontFamily, int fontSize, boolean bold, boolean italic,
        boolean underline, boolean strikeThrough, Boolean superscript) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.Color;

import javax.swing.JTextPane;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class Main {

    public static SimpleAttributeSet insertStyledString(JTextPane textPane, String text, SimpleAttributeSet attrSet,
            Color background, Color foreground, String fontFamily, int fontSize, boolean bold, boolean italic,
            boolean underline, boolean strikeThrough, Boolean superscript) {
        if (attrSet == null) {
            attrSet = new SimpleAttributeSet();
        }/*from  w w w . jav a  2  s.c  o  m*/

        if (background != null)
            StyleConstants.setBackground(attrSet, background);
        if (foreground != null)
            StyleConstants.setForeground(attrSet, foreground);
        if (fontFamily != null)
            StyleConstants.setFontFamily(attrSet, fontFamily);
        if (fontSize > 0)
            StyleConstants.setFontSize(attrSet, fontSize);
        if (bold)
            StyleConstants.setBold(attrSet, true);
        if (italic)
            StyleConstants.setItalic(attrSet, italic);
        if (underline)
            StyleConstants.setUnderline(attrSet, underline);
        if (strikeThrough)
            StyleConstants.setStrikeThrough(attrSet, strikeThrough);
        if (superscript != null) {
            if (superscript) {
                StyleConstants.setSuperscript(attrSet, true);
            } else {
                StyleConstants.setSubscript(attrSet, true);
            }
        }

        if (textPane != null) {
            Document doc = textPane.getDocument();
            try {
                doc.insertString(doc.getLength(), text, attrSet);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }

        return attrSet;
    }
}

Related

  1. centerText(JTextPane pane)
  2. createJTextPane(String text, Color backgroundColor)
  3. createTextPaneScrollPane(JTextPane textPane)
  4. getFontFamily(JTextPane textPane)
  5. getForegroundColor(JTextPane textPane)
  6. isSelectionBold(JTextPane textPane)
  7. overrideStyle(String styleName, JTextPane textPane, Style newStyle)
  8. saveFile(JTextPane txt)
  9. scrollTo(JTextPane textPane, int position)