1

I have I JTextPane and I'm using DefaultStyledDocument as a text model. I currenly use the JTextPane's default copy() and paste() methods for copying and pasting, but as you know they copy/paste plain text only. I need to be able to copy styled text from a browser for example, and paste it in the JTextPane preserving the styles.

Does anyone know how I can achieve this?

StanislavL
  • 56,971
  • 9
  • 68
  • 98
Igor
  • 1,532
  • 4
  • 23
  • 44

1 Answers1

0

When content in clipboard has representation which can be processed by the installed EditorKit then JTextPane automatically recognize the content and process the content updating Document accordingly.

E.g. if you set RTFEditorKit and paste content copied from MS Word (which has text/rtf flavor) the content will be processed correctly.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 1
    I have already set RTFEditorKit up and still only plain text is copied. – Igor Jan 27 '13 at 18:51
  • I also tried to paste this way: `String clipboardText = getClipboardContents();`, but as you can see I can only get plain unstyled text from the clipboard. – Igor Jan 27 '13 at 18:55
  • it depends on COPY as well. Where did you copy your content? RTF is supported in e.g. MS Word but is not supported from FF. You can also check the kit http://java-sl.com/advanced_rtf_editor_kit.html – StanislavL Jan 29 '13 at 08:12
  • 1
    I tried copying from MS Word and IE. I got unstyled text pasted both times. Can copy() be overriden to somehow include styles? – Igor Jan 29 '13 at 13:58
  • copy() cannot be overridden. It's not java part but the source application part. The only thing you can do is to check all available flavors and choose one where you can extract styles info. You can try the AdvancedRTFEditorKit I mentioned. For me copy/paste from MS Word works fine. – StanislavL Jan 30 '13 at 05:53