HTML JTextPane newline support

I am using JTextPane for HTML editing. When I enter newlines in the GUI component and call getText () on the JTextPane, I get a newline string. If I then create a new JTextPane and pass in the same text, the newlines are ignored.

Why doesn't JTextPane insert <br> tag on newline? Is there a good way to do 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);        

      

Output example:

<html>
  <head>

  </head>
  <body>
    Try entering some newline characters.
What gives?
  </body>
</html>

      

I realize I can convert newlines to HTML line breaks before calling setText, but that also converts newlines after HTML and BODY tags, and seems dumb.

+2


source to share


3 answers


I solved it, the problem was that I was plain text in setText. When invoked setText

, the result JTextPane.getText()

is well-formatted HTML with correct string encoding.

I believe when I call JTextPane.setText("Try entering some newline characters")

it sets HTMLDocument.documentProperties.__EndOfLine__

to "\ n". This document property constant is defined here .

The solution is for you to wrap the text in tags <p>

when you pass it to the JTextPane.setText () method (note that the style attribute is used for subsequent paragraphs):



textPane1.setText("<p style=\"margin-top: 0\">Try entering some newline characters</p>");

      

Or, after you pass in plain text, replace EndOfLineStringProperty (this is more of a hack, I wouldn't recommend it):

textPane1.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, "<br/>\n")

      

+8


source


It looks like the HTMLWriter class is eating the newline and not reading it or translating it to HTML (see line 483 in HTMLWriter). I don't see an easy way around this as it seems to be hardcoded to check for \ n. Perhaps you can set the DefaultEditorKit.EndOfLineStringProperty of the JTextPane document (via getDocument (). PutProperty) to <br> and then override setText to replace "\ n" with <br>. Although this will do what you suggested and add breaks between the html, head and body tags, so you might want to do the replacement only in the body tags. There doesn't seem to be a very straight forward way to do this.



0


source


I've been fighting this for half a day. This is still broken in Java 7. The problem is that a newline is being entered by the user into the JEditorPane (for HTML content type). I was only able to preserve the newline in HTML when I added the token key "\ r" by the user typed in "\ n" (still needs "\ n" to display the newline in the editor) and then replaced it with a "\ n" when I pulled out all the content and put it in another JEditorPane or whatever as the HTML I want. I've only tested this on Windows.

((AbstractDocument) jEditorPane.getDocument()).setDocumentFilter(new HtmlLineBreakDocumentFilter());

// appears to only affect user keystrokes - not getText() and setText() as claimed
public class HtmlLineBreakDocumentFilter extends DocumentFilter
{
    public void insertString(DocumentFilter.FilterBypass fb, int offs, String str, AttributeSet a)
                        throws BadLocationException
    {
        super.insertString(fb, offs, str.replaceAll("\n", "\n\r"), a); // works
    }

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
        throws BadLocationException
    {
        super.replace(fb, offs, length, str.replaceAll("\n", "\n\r"), a); // works
    }
}


String str = jEditorPane.getText().replaceAll("\r", "<br/>"); // works
jEditorPane2.setText(str);

      

0


source







All Articles