0

I have my custom JComponent which do a lot of drawing operations.

They also include drawStrings for text paragraphs, but now Id like to format the text (in a seperate window with jtextpane or another RTF editor) (bold, font-size, align, font-color, lists) and show this formatted text in my own component.

Id like to do that without bufferedimages because if i have a big jcomponent full with images I would get a overflow error (RAM)

Something like with AttributedString could work, but how?

I don't really know how to get the RTF-Text out of the Jtextpane (or if there is a better editor plz tell me) and draw it in the "paintComponent(Graphics g)" method.

The Editor is just used to format the text easily, not for the presentation (which is done with my own component)

Can anyone help me please?


Thanks for your help,

so i create an AttributeString and then apply the RTF-formattings in it?

do you know how I can extrude the RTF information to an adequate AttributedString? e.g. a function which transforms the formatted text to an AttributedString like

public AttributedString getAttrString(String plainRTF){
   ...
}

Thanks a lot

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • there is a link in my question to a sample implementation of such function. It is a bit simplified but should give you a general idea. – tenorsax Mar 19 '12 at 03:23

2 Answers2

1

Try this custom AdvancedRTFEditorKit http://java-sl.com/advanced_rtf_editor_kit.html

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

You can parse the structure of the underlying Document to get to the actual elements. Each Element has a set of attributes you can access with getAttributes().

You can build AttributedString using document.getText() and applying corresponding styles using AttributedString.addAttribute() method.

Then draw the string on Graphics. Check out this tutorial that uses TextLayout to render the AttributedString.

Take a look at styledDocumentToAttributedString() method here. It may be already implements what you need; it depends on the complexity of your string. Also, this discussion may be useful.

tenorsax
  • 21,123
  • 9
  • 60
  • 107