Jtextpane copy coloured text and paste - java

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

Related

JEditorPane unexpected behavior with new lines

I am trying to highlight text in a JEditorPane text field and save the index of the selected text. However I keep running into problems when I save the selections. The JEditorPane seems to save new lines in a different way each time. It will write a "\n", a "\r" and sometimes even a "\r\n" when the user presses the enter key. And sometimes the JEditorPane will even ignore carriage returns which causes wrong selection indices.
Is it possible to get a consistent behavior when the user presses the enter key?
Why are you using a JEditorPane. That is for displaying HTML and HTML generally doesn't have line breaks. You use the "br" tag in HTML. For normal text you should be using either a JTextPane or JTextArea.
I am trying to highlight text
The text in a string depends on the component you are using and whether you get the text from the Document or the component.
In general you should get the text from the Document since the Document only stores the "\n" so you should be able to calculate the offset correctly and highlight the text.
When you get the text from the component, the platform new line String is inserted in the text which would cause your offset problems.
See Text and New Lines for more information.

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)

Automatic line break in Java html text editor

I am creating a text editor in java that saves the text as html, but displays as plain text to the user. The user has the ability to change the color, alignment and style (bold and underline) of the text. The entire body of text is stored in the database as html in order to save the style adjustments. I am having an issue with newlines not being saved. So when the user is entering text, presses enter and puts text on a new line, it all gets put on one line after be saved and re-displayed. All the text is just being put inside on paragraph tag without any line breaks. I'm wondering if there's a way to tell the text editor to automatically insert line breaks for new lines?
The way I have my editor set up is a JTextPane using a HTMLEditorKit with content type set to text/html. I am using StyledEditorKit actions for changing the color and style (bold, underline) of the text and StyleConstants.setAlignment for changing the text alignment (I had some issues with the StyledEditorKit.AlignmentAction). Let me know if you need any specific source code.
try encapsulating your text inside an html < p > tag - it should maintain line breaks.
I assume there is a key down event for this kit? Assuming you have a KeyEvent handler you should be able to do something like this:
if(e.getKeyCode() == KeyCodes.KEY_ENTER){
editor.Text += "<br />";
}

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.

Scroll through JLabel held in JScrollPane

I have a JScrollPane that holds a JLabel using the following code:
//Create TEXT LOG JPanel
textLogPane = new JScrollPane(logLabel);
textLogPane.setPreferredSize(textLogPaneDim);
//textLogPane.setOpaque(true);
textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
textLogPane.getViewport().setBackground(Color.DARK_GRAY);
The JLabel, logLabel, is represented by a string with an HTML encoding using for carriage returns. I display certain images based on the contents of certain lines and I would like to be able to scroll the JScrollPane, textLogPane, to show that line whenever I am displaying that graphic. I know the contents of the line that I want to display but I can't seem to figure out how to get it to scroll down(or up) to the relevant line.
If need be I can change to something other than a JLabel as long as I can keep the HTML encoding and have it look just like multiple lines of text.
Sorry if this is a repeat I tried searching but couldn't find any results.
Thanks
You can do some custom maths and use scrollRectToVisible() in your viewport. I don't know how to compute the rect of a specific line in your JLabel. A better solution would be to stick your strings into a JList instead, perhaps with a custom renderer for the html, and use
list.ensureIndexIsVisible(list.getSelectedIndex());
You don't use "carriage returns" in HTML, you use "br" tags.
I would suggest you should probably be using a JTextPane for multiline text. I also find it easier to not use HTML, but instead to add strings with attributes. You can also insert icons into a JTextPane.
Read the section from the Swing tutorial on Using Text Components for a working example.

Categories

Resources