Java tutorial
//package com.java2s; import javax.swing.*; import javax.swing.text.*; public class Main { /** * Affect a stylised text into an instance of JTextPane. * @param textPane Instance of JTextPane to affect. * @param textArray Array of strings. * @param styleArray Array of styles. Must match the textArray. */ public static void setText(JTextPane textPane, String[] textArray, String[] styleArray) { StyledDocument doc = textPane.getStyledDocument(); try { doc.remove(0, doc.getLength()); // Erase all the previous text. for (int i = 0; i < textArray.length; i++) { int offset = doc.getLength(); javax.swing.text.Style style = textPane.getStyle(styleArray[i]); doc.insertString(offset, textArray[i], style); doc.setParagraphAttributes(offset, textArray[i].length(), style, true); } textPane.setCaretPosition(0); } catch (BadLocationException ignore) { ignore.printStackTrace(); } } }