I have this code to demonstrate the problem:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new JEditorPane("text/html", "Hello cruel world<br>\n<font color=red>Goodbye cruel world</font><br>\n<br>\nHello again<br>\n"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
If you select all the text that appears in the frame once the app starts, you can copy it and paste it into MS Word, Apple’s Pages, or Mail and the text is formatted correctly. But if you paste it into a pure text editor such as TextEdit, Smultron, or a Skype chat window all the pasted content is on one line.
What can I do to make the text copied to the clipboard able to be pasted with newlines preserved?
I’m running my code on Mac OS X 10.7
After getting no answers, I rolled up my sleeves and did a lot of research and learning. The solution is to make a custom TransferHandler for the component, and massage the HTML text manually. It wasn’t easy to work all this out, which could account for the zero answers I got.
Here’s a working solution: