This should get you started:
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style style1 = doc.addStyle("someCode", regular);
StyleConstants.setFontFamily(style1, "courier");
StyleConstants.setFontSize(style1, 12);
StyleConstants.setBold(style1, true);
Style style2 = doc.addStyle("plainText", style1); // inherits from style1
StyleConstants.setFontFamily(style2, "arial");
StyleConstants.setFontSize(style2, 12);
StyleConstants.setItalic(style2, true);
doc.insertString(0, "hello world");
doc.setCharacterAttributes(0, 11, doc.getStyle("plainText"), false);
//or doc.setCharacterAttributes(0, 11, style1, false);
doc.setCharacterAttributes(3, 5, doc.getStyle("someCode"), false);
//or doc.setCharacterAttributes(3, 5, style2, false);
“hello world” will be bold
“lo wo” will be both bold and italic
Be sure to run ALL CODE from the EDT, because JTextPane is VERY unreliable when accessed from other threads.