Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JEditorPane: scroll to line but display at top

843806May 30 2008 — edited May 30 2008
I'm displaying HTML in an editor pane with tags that allow the user to jump to different sections. Jumps going up to a previous section work as expected in that the located section is displayed at the top of the pane. However, jumps going down to a next setion only shows the top line of the section at the bottom of the pane, instead of displaying the section at the top of the pane (i.e., typical browser behavior).

This topic has been discussed many times but my searches did not come up with a solution to this particular issue. While I did come up with a solution it is a bit of a hack so I wanted to see if anyone had a cleaner approach. Since going up works fine and only going down has the issue, my current approach is to always jump to the bottom of the page first then jump to the appropriate section.

I basically implemented the following in my HyperlinkListener:
HTMLDocument doc = (HTMLDocument)editorPane.getDocument();
Element text = doc.getElement(mDescText);
if (text != null)
{
   try
   {
      editorPane.setCaretPosition(doc.getLength());
      Rectangle mRectangle1 = editorPane.modelToView(doc.getLength());
      editorPane.scrollRectToVisible(mRectangle1);
                            
      editorPane.setCaretPosition(text.getStartOffset());
      Rectangle mRectangle2 = editorPane.modelToView(text.getStartOffset());
      editorPane.scrollRectToVisible(mRectangle2);
   }
   catch (BadLocationException e)
   {
                 
   }
}
While it works it would seem that there should be a better way to implement this. Let me know if you have a better approach. Thanks.

Edited by: 021336 on May 30, 2008 7:01 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 27 2008
Added on May 30 2008
2 comments
370 views