adhg
adhg

Reputation: 10923

how to append css/html table in jtextpane

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:

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

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

Upvotes: 1

Views: 2226

Answers (1)

Gianmarco
Gianmarco

Reputation: 2552

You have to declare what type of text you are using in the JTextPane

jtextPane.setContentType("text/html");

If this don't work, try also to include your text with correct <html> that should do. I had the same problem time ago, I am looking for a specific code.

Upvotes: 1

Related Questions