Search code examples
javahtmlstylesjtextpane

JTextPane with HTML, Why do certain inline style attributes get selectively removed?


I am currently working with a JTextPane with html in it. I set its content type to html and everything worked just fine... or so I thought.

The function of the JTextPane is to output paragraphs(with

tags), each in a different color. Each set of

tags comes equipped with an inline style attribute.

Now I am printing the

tags like this:

String myLine = "<P style=\"color:blue;" +
        "padding-left:25px;" +
        "text-indent:-25px;" +
        "font-family:Courier New;" +
        "font-size:11;"  +
        "\">" ;
doc.insertBeforeEnd(body, myLine);

Where doc is the JTextPane HTMLDocument of the JTextPane, body is the body element in my HTMLDocument.

It outputs everything just fine in the JTextPane, the text is blue, courier, 11 size with a hanging indent. PERFECT!

You would think that if you recovered the text once more you would see that P tag just the way you built it. So I recover the html inside it using getText() method: Reality

<p style="text-indent: -25px; padding-left: 25px">

when I was actually expecting to see this: Expectation

<p style="color:blue; text-indent: -25px; padding-left: 25px; font-family:Courier New; font-size:11;">

Why does it do this? Is there a way to avoid this? If I had to guess, it seems that Java extracts the text attributes so that it can more efficiently process those attributes by its own means.

However, the reason I am asking this is because once I began customizing my JTextPane more in depth, the coloring started becoming unreliable. I would rather just have the attributes directly on the inline style.

Thanks in advance, I greatly appreciate your help.


EDIT: Someone asked to see full html output before and after

tags were added.

Before:

<html>
  <head>

  </head>
  <body>

  </body>
</html>

Now I execute this code in java:

String htmlLine = "<p style=\"color:blue; " +
                             "text-indent: -25px; " +
                             "padding-left: 25px; " +
                             "font-family:Courier New; " +
                             "font-size:11;\" >" ;
try {
    doc.insertBeforeEnd(body, htmlLine);
} catch (Exception e) {
    System.err.println(e);
}

After:

<html>
  <head>

  </head>
  <body>
    <p style="text-indent: -23px; padding-left: 25px">
      First Text
    </p>
  </body>
</html>

Solution

  • As per the Java doc of insertBeforeEnd()

    Unlike the insertAfterEnd method, new elements become children of the specified element, not siblings.

    It means that the inserted elements are becoming the children and inherit the style of their parents. Internally while inserting, the HTMLDocument removes duplicate style info from the children which are already present for the parent. So this is the reason you are getting

    <p style="text-indent: -25px; padding-left: 25px">
    

    Instead of

    <p style="color:blue;
              text-indent: -25px;
              padding-left: 25px;
              font-family: Courier New;
              font-size:11;" >
    

    Finally the cause which have been in your case is

    • You had set the same style for the parent.