JTextPane getSelectedText with HTML tags
I have a JTextPane containing HTML formatted text. I would like to allow the user to copy text from the panel, replacing
<br />
with a symbol \n
. For this I am using TransferHandler.
TransferHandler works great. My only problem is that if I want to get the HTML formatted content of the JTextPane, I have to use its getText () method. If I use the getSelectedText () method, I only get plain text without tags <br />
, which is a problem as I want to preserve the formatting when the user copies the contents of the panel.
Is there a way to get the selected text of the JTextPane with its html tags?
I found a solution that is pretty hackish.
Instead of formatting the displayed text with tags, <br />
I put each line on its own <div>
. This way, when you copy text from the JTextPane, the embedded text will retain the string formatting.