代码之家  ›  专栏  ›  技术社区  ›  Sam Barnum

HTML JTextPane换行支持

  •  3
  • Sam Barnum  · 技术社区  · 15 年前

    我正在使用JTextPane编辑HTML。当我在GUI组件中输入换行符并在JTextPane上调用getText()时,我会得到一个包含换行符的字符串。如果我随后创建一个新的JTextPane并传入相同的文本,则会忽略换行符。

    为什么JTextPane不插入一个<br>输入换行符时标记?有没有好的解决办法?

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

    样本输出:

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

    我意识到我可以在调用setText之前将换行符转换为HTML换行符,但这也会在HTML和BODY标记之后转换换行符,而且看起来很愚蠢。

    3 回复  |  直到 15 年前
        1
  •  8
  •   Sam Barnum    11 年前

    我已经解决了这个问题,问题是我在setText中传递的纯文本。如果我打电话给 setText JTextPane.getText() 格式良好的HTML,换行符编码正确。

    我相信当我打电话的时候 JTextPane.setText("Try entering some newline characters") HTMLDocument.documentProperties.__EndOfLine__ 到“\n”。此文档属性常量已定义 here .

    <p>

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

    或者,在传入纯文本后,替换EndOfLineStringProperty(这更像是一种黑客行为,我不推荐):

    textPane1.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, "<br/>\n")
    
        2
  •  0
  •   Jeff Storey    15 年前

    它看起来像是HTMLWriter类吃掉了新行,而没有读取它或将它转换为HTML(参见HTMLWriter中的第483行)。我看不到一个简单的解决方法,因为它似乎是硬编码的,用于检查“\n”。您可以将JTextPane文档的DefaultEditorKit.EndOfLineStringProperty属性(通过getDocument().putProperty)设置为<br>然后重写setText以将“\n”替换为<br>。虽然这将按照您的建议进行,并在html、head和body标记之间添加分隔符,因此您可能只希望在body标记中进行替换。似乎没有一个非常直接的方法可以做到这一点。

        3
  •  0
  •   Lund Wolfe    13 年前

    我为这个挣扎了半天。这在Java7中仍然被打破。问题在于将用户输入的新行保留在JEditorPane中(对于HTML内容类型)。只有当我在用户输入的“\n”(仍需要“\n”在编辑器中显示新行)处添加标记键“\r”时,我才能在HTML中保留新行,然后当我将整个内容取出并放入不同的窗格或任何所需的HTML中时,将其替换为“\n<br/>”。我只在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);