Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:32:24+00:00 2026-06-18T02:32:24+00:00

I am currently working with a JTextPane with html in it. I set its

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:32:26+00:00Added an answer on June 18, 2026 at 2:32 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im currently working on adding content to an external ajax file being loaded with
Currently working on a project where we need to drop in various static html
Currently working on a legacy script (PHP) for one of my customers. He just
Currently working on a wp7 App, its quite basic. the user has a counter
Im currently working on an app which lists a set of objects in a
Currently working with converting SQLException error messages into messages that are more useful for
Currently working with the following package structure: /package __init__.py final.py /write __init__.py write.py /data
Currently working for the first time with JSON and with little experience of jQuery.
Currently working in the deployment of an OFBiz based ERP, we've come to the
Currently working on a VBScript to automate some of the dirty PST ingestion work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.