Modifying font and color on a JEditorPane

Hi,

how can I read and write the color and font on a JEditorPane for a given selection? Currently I am using RTF content. So the underlying document is a StyledDocument and the EditorKit is an RTFEditorKit, if that helps.

I found the getFont() method on the StyledDocument, that takes an AttributeSet. But I have no idea, where to get that from. And I also need to set the font (as well as color).

Also the font looks slightly different to how the exact same RTF looked in my old VB6 application (messes up formatting through tabulators). Is there a trick to fix this?

Any help is greatly appreciated. Thanks in advance.

Marvin

Bump

Is the JEditorPane not meant for changing the font or color properties of selected text without knowing the underlying editor kit? Do I understand the component wrong?

You could use the JTextPane, it is easier to use for changing specific text to any formatting you want.

Thanks for the reply, ra4king.

Unfortunately I cannot use JTextPane, because I need to use RTF as underlying format.

But I have discovered the solution. This is the source to change arbitrary text attributes on a JEditorPane.


StyledDocument doc = (StyledDocument)editor.getDocument();

SimpleAttributeSet atts = new SimpleAttributeSet();
StyleConstants.setUnderline( atts, true );

doc.setCharacterAttributes( editor.getSelectionStart(), editor.getSelectionEnd() - editor.getSelectionStart(), atts, false );

Now I only need a way to make the JEditorPane wrap at whitespaces rather than at any position in a word. Do you know, how to do that? JTextPane does that out of the box. And since it is nothing more than a JEditorPane extension, there must be a way.

Thanks,

Marvin

You’re asking for highly specific information about the guts of these components. And these are some of the most archane an obtuse bits of the Swing framework. I’ve worked with them before but it’s been a couple of years. Your best bet is to google for the ‘swing text architecture’ documents on the web and examine the source code of each implementation. There are a few open source syntax highlighting components that may help like jsyntaxpane and fifesoft syntax component. Sorry I can’t help any more than that.