How can I copy/paste styled text from/to clipboard? - java

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?

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.

Related

How to save and open jtextpane content to/from an external file which contains Bold,Italics,Underline text, images and other styled content?

I am developing a Wordpad application using Java Swing.
I am using JTextPane component. I have added the code for Bold, Italics, Underline. I am adding the code to insert images in all the formats.
My expected scenario: I want to save the document with all these styled content with the extension '.doc' (or)'.docx'. And I want to read and open the document which contains these types of styled content like Bold, Italic, underline text and images, bullet points etc..
I don't think it can be done with HTMLEditorKit().
Can anyone help with the sample code for saving and reading these styled contents to/from external file?
Thanks in advance.

How to get the content with all attributes of a JTextPane?

I have a JTextPane and on a ActionEvent, I want to save the content of the JTextPane (with the colors, text and other attributes) in another class (as a variable). Later on I want to exchange the content of the JTextPane with the one of the variable.
I have tried to use:
(StyledDocument)myTextPane.getDocument()
and
myTextPane.getStyledDocument()
but both didn't work.
I thought about just give the whole JTextPane over, but apperently the pane gets still updated in the other class...
EDIT: I would only use the content to show it (later) in the JTextPane again. I won't be saving it in a file or something similar.
Actually it depends on EditorKit you use. Each kit has own format to store/load content.
For the simplest case (e.g. if you use HTMLEditorKit) you can use getText()/setText().
If you need own format and would like to store the content yourself read this
Also you can try to use the AdvancedRTFEditorKit to store the content as RTF (default RTFEditorKit is limited)

Jtextpane copy coloured text and paste

I have a JTextPane which displays colored text . I use the followong piece of code to get text from JTextPane.
String temp = pane.getDocument().getText(0,pane.getDocument().getLength());
However when i try to set the temp variable content to pane again,
pane.select(0,pane.getDocument().getLength());
pane.replaceSelection(temp);
by this way i lose the color and get white text. Is there anyway where i can maintain the color of text without copying the content to clipboard.
Please help.
Actually it depends on EditorKit you use. The first part returns text (with styled info) of the selected fragment. E.g. in the RTFEditorKit it would be rtf content of the document's fragment.
The second part is not correct. Replace selection can't process the content properly. I guess in case of the RTFEditorKit it would be all the text with formatting inserted in the pane.
I would use
pane.setText(temp);
instead. If you need to insert the styled fragment use kit.read(...) passing the temp in the call
You can try the Kit as alternative of the default RTFEditorKit and see what happens
UPDATE: Sorry original comment was incorrect a bit. The code should be
pane.getEditorKit().read(
new StringReader(temp),
pane.getDocument(),
pane.getDocument().getLength())

JAVA - set font of selectedtext in a JEditorPane

How can I set the font/bold/italic/underline whatever of some text in a JEditorPane?
I am trying to create a simple document editor, but i can only figure out how to set the font/bold/italic/whatever of the ENTIRE JEditor/JText pane.
RTF is preferred, HTML is fine as well.
JEditorPane.getDocument() returns an instance of StyledDocument. StyledDocument has setCharacterAttributes() method to set the font properties.
Related example: http://www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetCharacterAttributesAttributeSetattrbooleanreplace.htm

Java JTextPane Save

i was trying to do some simple text formatting using JEditorPane but then as knowledge grew i found JTextPane easier to implement and more robust.
my query is how do i save the formatted text in JTextPane to file? it should be RTF or HTML or other.. as this file is not opened by the application again.
it is a chat history text file with formatted text.
thank you.
You have write method for HTML and RTF from StyledEditorKit. See HTMLEditorKit.write and RTFEditorKit

Categories

Resources