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 1003857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:02:30+00:00 2026-05-16T08:02:30+00:00

This seems like such a simple question, but I’m having such difficulty with it.

  • 0

This seems like such a simple question, but I’m having such difficulty with it.

Problem:

I have some text to insert into an HTMLDocument. This text sometimes specifies some html as well. E.G.:

Some <br />Random <b>HTML</b>

I’m using HTMLEditorKit.insertHTML to insert it at a specified offset. This works fine, unless the offset is at the begining of the doc (offset = 1). When this is the case the text gets inserted into the head of the document instead of the body.

Example:

editorKitInstance.insertHTML(doc, offset, "<font>"+stringToInsert+"</font>", 0, 0, HTML.Tag.FONT);

I use the font tag so I now what I’m inserting will be in a font tag with no attributes so it won’t effect the format. I need to know this because the last parameter, insertTag, is required and I can’t know the contents of stringToInsert until runtime. If there is already text in the doc (such as “1234567890”) then this is the output:

<html>
  <head>

  </head>
  <body>
    <p style="margin-top: 0">
      1234567890 <font>something <br />Some <br />Random <b>HTML</b></font>
    </p>
  </body>
</html>

However if the offset is 1 and the document is empty this is the result:

<html>
  <head>

<font>Some <br />Random <b>HTML</b></font>
  </head>
  <body>
  </body>
</html>

Other Notes:

  • This is all being done on the
    innerdocument of a JEditorPane. If
    there is a better way to replace text
    in a JEditorPane with potential
    HTML I would be open to those ideas
    as well.

Any help would be appreciated. Thanks!

  • 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-05-16T08:02:31+00:00Added an answer on May 16, 2026 at 8:02 am

    There are several things you should know about the internal structure of the HTMLDocument.

    • First of all – the body does not start at position 0. All textual content of the document is stored in an instance of javax.swing.text.AbstractDocument$Content. This includes the title and script tags as well. The position/offset argument of ANY document and editor kit function refers to the text in this Content instance! You have to determine the start of the body element to correctly insert content into the body. BTW: Even though you didn’t define a body element in your HTML, it will auto-generated by the parser.
    • Simply inserting at a position tends to have unexpected side effects. You need to know where you want to put the content in relation to the (HTML) elements at this position. E.g. if you have the following text in your document: “...</span><span>...” – there is only one position (referring to the Content instance) for “at the end of the first span”, “between the spans” and “at the start of the second span”. To solve this problem there are 4 functions in the HTMLDocument API:
      • insertAfterEnd
      • insertAfterStart
      • insertBeforeEnd
      • insertBeforeStart

    As a conclusion: for a general solutions, you have to find the BODY element to tell the document to “insertAfterStart” of the body and at the start offset of the body element.

    The following snipped should work in any case:

    HTMLDocument htmlDoc = ...;
    Element[] roots = htmlDoc.getRootElements(); // #0 is the HTML element, #1 the bidi-root
    Element body = null;
    for( int i = 0; i < roots[0].getElementCount(); i++ ) {
        Element element = roots[0].getElement( i );
        if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) {
            body = element;
            break;
        }
    }
    htmlDoc.insertAfterStart( body, "<font>text</font>" );
    

    If you’re sure that the header is always empty, there is another way:

    kit.read( new StringReader( "<font>test</font>" ), htmlDoc, 1 );
    

    But this will throw a RuntimeException, if the header is not empty.

    By the way, I prefer to use JWebEngine to handle and render HTML content since it keeps header and content separated, so inserting at position 0 always works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like such a simple question, but I have been trying for an
This seems like such a simple question. I have several Edit boxes on my
This seems like such a simple issue but I cannot find an elegant solution.
This seems like a simple question, but I haven't been able to find the
This seems like a simple question to ask but, is it generally a good
This seems like a simple question, but I can't find it with the Stack
This seems like a simple question but I can't seem to find an answer
This seems like a fundamentally simple question. I have a WinForms dialog box with
I know this is such a lame question to ask, but it seems like
This is driving me absolutely bonkers, and it seems like such a simple thing

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.