Here you can find the source of overrideStyle(Style originalStyle, Style newStyle)
public static void overrideStyle(Style originalStyle, Style newStyle)
//package com.java2s; 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 { protected static StyleContext styleContext = new StyleContext(); public static void overrideStyle(String styleName, StyledDocument document, Style newStyle) { Style style = document.getStyle(styleName); if (style == null) { style = styleContext.addStyle(styleName, null); document.addStyle(styleName, style); }/* w w w .j a v a 2s. c o m*/ overrideStyle(style, newStyle); } public static void overrideStyle(String styleName, JTextPane textPane, Style newStyle) { Style style = textPane.getStyle(styleName); if (style == null) { style = styleContext.addStyle(styleName, null); textPane.addStyle(styleName, style); } overrideStyle(style, newStyle); } public static void overrideStyle(Style originalStyle, Style newStyle) { overrideStyleElement(originalStyle, newStyle, StyleConstants.FontFamily); overrideStyleElement(originalStyle, newStyle, StyleConstants.Family); overrideStyleElement(originalStyle, newStyle, StyleConstants.FontSize); overrideStyleElement(originalStyle, newStyle, StyleConstants.Size); overrideStyleElement(originalStyle, newStyle, StyleConstants.Foreground); overrideStyleElement(originalStyle, newStyle, StyleConstants.Background); overrideStyleElement(originalStyle, newStyle, StyleConstants.Bold); overrideStyleElement(originalStyle, newStyle, StyleConstants.Italic); overrideStyleElement(originalStyle, newStyle, StyleConstants.StrikeThrough); overrideStyleElement(originalStyle, newStyle, StyleConstants.Subscript); overrideStyleElement(originalStyle, newStyle, StyleConstants.Superscript); overrideStyleElement(originalStyle, newStyle, StyleConstants.Underline); overrideStyleElement(originalStyle, newStyle, StyleConstants.RightIndent); overrideStyleElement(originalStyle, newStyle, StyleConstants.LeftIndent); overrideStyleElement(originalStyle, newStyle, StyleConstants.SpaceAbove); overrideStyleElement(originalStyle, newStyle, StyleConstants.SpaceBelow); overrideStyleElement(originalStyle, newStyle, StyleConstants.FirstLineIndent); overrideStyleElement(originalStyle, newStyle, StyleConstants.LineSpacing); overrideStyleElement(originalStyle, newStyle, StyleConstants.Alignment); overrideStyleElement(originalStyle, newStyle, StyleConstants.BidiLevel); overrideStyleElement(originalStyle, newStyle, StyleConstants.Orientation); overrideStyleElement(originalStyle, newStyle, StyleConstants.TabSet); } public static boolean overrideStyleElement(Style originalStyle, Style newStyle, Object key) { // System.out.println( "DocumentUtils: setting " + key + " to " + newStyle.getAttribute( key ) ); originalStyle.removeAttribute(key); Object attribute = newStyle.getAttribute(key); if (attribute != null) { originalStyle.addAttribute(key, attribute); return true; } return false; } }