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

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)

Related

Format an editable textarea

At the moment I try to find a method to create a TextArea that I can edit and simply (without cheating) apply syntax highlighting to. Is that possible without the need of a custom Component? I already managed to format a text using a JEditorPane, but I am not sure how to implement that the text is dynamicly highlighted... And that efficiently. Is that possibly without an enormous amount of coding?
You can't use a JTextArea since it does not support text attributes.
Instead you can use a JTextPane. Read the section from the Swing tutorial on Text Component Features for more information and examples.

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())

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

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.

How can I print a RTF formatted String to a JLabel?

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
Try this custom AdvancedRTFEditorKit
http://java-sl.com/advanced_rtf_editor_kit.html
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.

Java JTextPane Change Font of Selected Text

I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font?
textpane.getSelectedText().setFont() won't work. (Even with font-family)
You can change JTextPane's font only as a whole, it doesn't do rich text.
There's a Document underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument(). You want to cast that to a StyledDocument if you can, and then you can do things like setCharacterAttributes to a given run of characters.
There are some (hopefully) helpful examples in the Java tutorial at http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html .
You can do this by using JTextPane. It is impossible to do this using JTextArea. . Here is a best example on how to use JTextPane.
Sample: http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html
Code: http://download.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java

Categories

Resources