1

I'm trying to 'append' an css/html table to a jtextpane.

When I do: setText like this:

        jtextpane.setText(css)

I get the desired result [perfect!]:

enter image description here

but when I try to append the text to the jtextpane like this:

        int len = jtextpane.getDocument().getLength();
        jtextpane.setCaretPosition(len); 
        jtextpane.replaceSelection(css);

I get the html code embedded like this: enter image description here

Q: how to append table's result (not the code) in the jtextPane? I assume I'm doing something wrong with the replaceSelection?! Thanks in advance

EDIT - additional information:

  • To append all text information to the jtextpane I'm using the following static method:
public static void appendToPane(JTextPane jtextpane, String userText, Color color)
{
  StyleContext sc = StyleContext.getDefaultStyleContext();
  AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color);
  aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Myriad Pro");
  aset = sc.addAttribute(aset, StyleConstants.FontSize, 20);
  int len = jtextpane.getDocument().getLength();
  jtextpane.setCaretPosition(len);
  jtextpane.setCharacterAttributes(aset, false);
  jtextpane.replaceSelection(userText);
}
  • on instantiation of the jtextpane I have:

    jtextpane.setContentType("text/html");

  • the original css string is this:

table.imagetable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #999999; border-collapse: collapse; } table.imagetable th { background:#b5cfd2 url('cell-blue.jpg'); border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } table.imagetable td { background:#dcddc0 url('cell-grey.jpg'); border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; }

4

1 回答 1

1

您必须在 JTextPane 中声明您使用的文本类型

jtextPane.setContentType("text/html");

如果这不起作用,请尝试在您的文本中包含<html>应该做的正确内容。我前段时间遇到了同样的问题,我正在寻找一个特定的代码。

于 2013-09-09T15:48:55.080 回答