Set Font In JEditorPane Or JTextPane

Setting the font of either a JEditorPane or JTextPane is not possible by doing it directly. However, you can do that by setting it in the EditorKit class. This post uses the RTFEditorKit class as an example of the editor kit used by a JEditorPane.

I created a custom class that inherits the RTFEditorKit to make things simpler. Just call initializeDefaultFont() to set the font to the JEditorPane. However, a very important thing to note is that this method should only be called after the RTFEditorKit object is set to the JEditorPane.setEditorKit() method. If the initializeDefaultFont() is called before that, the font will not be set.

To use the class. Check the sample code below.

And the MyRTFEditor.java class is below.

Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *