Getting the current offset in the HTMLDocument of a JTextComponent
In a Swing application I'm using a JTextPane with an HTMLDocument backend. At one point I'm inserting some sort of placeholders programmatically into the document with
document.insertString(...)
For some reason, using the
HTMLEditorKit.insertHTML()
method doesn't make the text appear in the JTextPane.
What I'd like to know is what position I'm inserting to in the document, th开发者_开发问答at is, the HTML document which is written in the background. I can say
JTextPane.getCaretPosition()
but this only gives me the offset in the document visible to the frontend, not the resulting one.
It has no sence because the result document's html text can be different. E.g. end of line could be "\n" or "\r\n" depending on OS and JVM settings. HTML skips e.g. double spaces or spaces between tags. Two tags without space between them and with a line break between them has the same offset in the Document. You can add a custom tag with specific id and search the result document's html text for the id.
精彩评论