1

I have this code to display HTML formatted string:

JTextPane pane = new JTextPane();
        pane.setContentType("text/html");
        pane.setEditable(false);
        pane.setText(
            "<html>" +
                "<body style='font-size:18px'>" +
                    "<h1>Error:</h1>" +
                    "<p>" +
                        "Could not read the file <code>none.txt</code>. " +
                        "Perhaps it does not exist in the specified location, " +
                        "or possibly there is no access to it" +
                    "</p>" +
                "</body>" +
            "</html>");
        add(pane);

But this is the output:

enter image description here

You can see that the none.txt string doesn't inherit the font size of its enclosing paragraph, although this is what's supposed to happen in HTML (see jsfiddle).

How do I fix this?

Tar
  • 8,529
  • 9
  • 56
  • 127
  • Perhaps end the body tag before the tags, then restate the body tag afterwards? Example: "Could not read the file none.txt – joey942 Dec 14 '16 at 13:46
  • @joey942, I don't see the sense in it. Why excluding `none.txt` from `` should work, and how can we have more than one ``? anyway, tried that, it completely ignores the whole content of `

    `, and all I see is the "Error:" text.

    – Tar Dec 14 '16 at 13:56
  • Then perhaps do this:
    none.txt
    Then in CSS, #notbody{ // set font size to what you want it to be} These are just suggestions, I have not tried them out myself.
    – joey942 Dec 14 '16 at 14:01
  • I may have found something for you, this may help: http://stackoverflow.com/questions/16201948/how-to-exclude-particular-class-name-in-css-selector It has information on the not selector. – joey942 Dec 14 '16 at 14:08

1 Answers1

3

Definitely a bug. You can work around it by adding explicit inheritance in the CSS, with a <style> element:

pane.setText(
    "<html>" +
        "<style>\ncode { font-size: inherit; }\n</style>" +
        "<body style='font-size:18px'>" +
            "<h1>Error:</h1>" +
            "<p>" +
                "Could not read the file <code>none.txt</code>. " +
                "Perhaps it does not exist in the specified location, " +
                "or possibly there is no access to it" +
            "</p>" +
        "</body>" +
    "</html>");
VGR
  • 40,506
  • 4
  • 48
  • 63
  • That's annoying. Now I have to add `inherit` for every kind of block I may ever use? `bold`, `code`, etc... ? – Tar Dec 14 '16 at 20:55
  • I don’t think `` needs it. Probably just the ones which change the font family. ``, `
    `, ``, and possibly ``.
    – VGR Dec 14 '16 at 21:40