Java JTextPane addStyle(JTextPane textPane, String name, Color foreground)

Here you can find the source of addStyle(JTextPane textPane, String name, Color foreground)

Description

add Style

License

Open Source License

Declaration

public static void addStyle(JTextPane textPane, String name, Color foreground) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import javax.swing.JTextPane;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class Main {
    public static void addStyle(JTextPane pane, String name, Color foreground, Color background) {
        StyledDocument doc = pane.getStyledDocument();
        StyleContext context = StyleContext.getDefaultStyleContext();
        Style defaultStyle = context.getStyle(StyleContext.DEFAULT_STYLE);
        Style style = doc.addStyle(name, defaultStyle);
        StyleConstants.setForeground(style, foreground);
        StyleConstants.setBackground(style, background);
    }//from  w  ww . jav a2s.c o  m

    public static void addStyle(JTextPane textPane, String name, Color foreground) {
        addStyle(textPane, name, foreground, null, false);
    }

    public static void addStyle(JTextPane textPane, String name, Color foreground, Color background, boolean bold) {
        StyledDocument doc = textPane.getStyledDocument();
        StyleContext context = StyleContext.getDefaultStyleContext();
        Style def = context.getStyle(StyleContext.DEFAULT_STYLE);
        Style style = doc.addStyle(name, def);
        if (foreground != null)
            StyleConstants.setForeground(style, foreground);
        if (background != null)
            StyleConstants.setBackground(style, background);
        StyleConstants.setBold(style, bold);
    }
}

Related

  1. appendAsGreen(JTextPane pane, String fontName, int fontSize, String s)
  2. appendAsMessage(JTextPane pane, String fontName, int fontSize, String s)
  3. appendString(String str, JTextPane jtext)
  4. appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)