Here you can find the source of addStyle(JTextPane textPane, String name, Color foreground)
public static void addStyle(JTextPane textPane, String name, Color foreground)
//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); } }