setting JTextPane to content type HTML and using string builders

orange picture orange · Jan 30, 2012 · Viewed 38.2k times · Source

I'm using string builders to append text to my JTextPane, I've set content type as pane.setContentType("text/html"); but the no text actually appears on my JTextPane.

This is an example of my append:

buildSomething.append("<b style=\"color:pink\">"+Birthday+"</span>");

Is there something I'm doing severely wrong? And how do I go about fixit it?

Answer

Joop Eggen picture Joop Eggen · Jan 30, 2012

Every time JTextPane.setText(...) is called a new content type is determined. Start the text with "<html>" and you've got HTML.

A new document is created, in your case HTMLDocument.


@mKorbel: the following creates every time HTML for the JTextPane.

    buildSomething.append("<html>");
    buildSomething1.append("<html>");
    for (int i = 0; i < 10; i++) {
        buildSomething.append("<span style=\"color:red\">" + myBirthday + "</span>");
        buildSomething1.append("<b style=\"color:blue\">" + myBirthday + "</b>");
    }