Search code examples
javafontscomboboxjeditorpaneselectedtext

Get font of selected text in JEditorPane


Basically the question says it all;

I have a JEditiorPane with the content type 'text/html'. I have created a font family and font size combo box and enabled them using the StyledEditorKit actions. This works great and I can change the font attributes of selected text (you what it does...)

The only thing is when I select the text it's a bit confusing because the ComboBox's still show the users previous selection. I would love to implement the functionality to update the combo box values according the selected text but cannot figure out how to get the selected texts font! I've tried all sorts of crafty workarounds - non of which have worked

Any Help appriciated

Thanks In Advance

Andy


Solution

  • Add a CaretListener. On each caretUpdate use the code

    AttributeSet attrs=((StyleEditorKit)editorPane.getEditorKit()).getInputAttributes()
    StyleConstants.getFontFamily(attrs);