I'm using a JTextPane to edit HTML. When I enter newlines in the GUI component and call getText() on the JTextPane, I get a string with newline characters. If I then create a new JTextPane and pass that same text in, the newlines are ignored.
Why doesn't JTextPane insert a <br> tag when a newline is entered? Is there a good workaround for this?
JTextPane test = new JTextPane();
test.setPreferredSize(new Dimension(300, 300));
test.setContentType("text/html");
test.setText("Try entering some newline characters.");
JOptionPane.showMessageDialog(null, test);
String testText = test.getText();
System.out.println("Got text: " + testText);
// try again
test.setText(testText);
JOptionPane.showMessageDialog(null, test);
testText = test.getText();
System.out.println("Got text: " + testText);
Sample output:
<html>
<head>
</head>
<body>
Try entering some newline characters.
What gives?
</body>
</html>
I realize I could convert newlines to HTML line breaks before calling setText, but that would convert the newlines after the HTML and BODY tags as well, and seems dumb.