The option is: JTextPanet
: has a Document to manage data and Editor kit to read and write text with editing capability even with styling( e.g, with font type and color foreground). You can also implement undo-redo options too.
However to satisfy your question i am writing a small snippets with StyleDocument
to show how it works.

StyledDocument styleDocument = jTextPane1.getStyledDocument();
Style primaryStyle = styleDocument.addStyle("Primary", null);
Style secondaryStyle = styleDocument.addStyle("Secondary", primaryStyle);
StyleConstants.setFontFamily(primaryStyle, "American Captain");
// use font-family from your platform
StyleConstants.setFontSize(primaryStyle, 24);
StyleConstants.setFontFamily(secondaryStyle, "Bira PERSONAL USE ONLY");
// use font-family from your platform
StyleConstants.setFontSize(secondaryStyle, 20);
StyleConstants.setForeground(primaryStyle, new Color(0x552AFF));
StyleConstants.setForeground(secondaryStyle, Color.black);
try {
styleDocument.insertString(0, "Title with American Captain font\n\n", primaryStyle);
styleDocument.insertString(styleDocument.getLength(), "Font demonstration with JTextPane. "
+ "Seriously, it is powerful and has the power to do all kind of styling with text. "
+ "check it out, check its mighty power and be embrassed\n", secondaryStyle);
} catch (BadLocationException ex) {
Logger.getLogger(JTextPaneTest.class.getName()).log(Level.SEVERE, null, ex);
}
Every Swing Text component is associated with a model known as Document to manage data. What we have done in this code is that, we set different style instances to the StyleDocument
associating with the JTextPane
component. I have assumed that you know how to add the JTextPane
to the JFrame
and show it. The two different style primary
and secondary
has different foreground and font family: "American Captain"
and "Bira PERSONAL USE ONLY"
. You can replace them with font-family available in your platform including serif
and sans-serif
. Remember that these style created by StyleDocument.addStyle()
has parent-child hierarchy relation. The child's style attributes(Foreground, background, font size, font style) will override the parent's attributes.
However Please go through these tutorial article:
- Using Text Components And
- How to Use Editor Panes and Text Panes